-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
116 lines (96 loc) · 3.85 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ggspark
<!-- badges: start -->
<!-- badges: end -->
The goal of ggspark is to help create ggplot2 functions that help with creating sparkline plots in the style of Edward Tufte, such as this one
![Original sparklines, source Edward Tufte.](https://s3.amazonaws.com/edwardtufte.com/sparklines_hemodynamics_3.jpg)
Thus, the package has two main functions: `stat_interquartilerange()` that draws a `geom_ribbon()` between the 1st and 3rd quartile of the variable in the y axis, and `stat_sparklabels()` that draws points or text labels in the beginning, min, max, and end points of the variable in the y axis.
## Installation
You can install the development version of ggspark like so:
``` r
devtools::install_github("marcboschmatas/ggspark")
```
## Example
The `stat_sparklabels()` function needs a colour scale with three values. The first one will be using for the start and end points of the line, the second one for the max, and the third one for the min.
```{r example}
library(ggplot2)
library(ggspark)
ggplot(airquality, aes(Day, Wind, group = Month)) +
stat_interquartilerange(geom = "ribbon",
show.legend = FALSE) +
geom_line() +
stat_sparklabels(geom = "label",
show.legend = FALSE) +
scale_colour_manual("", values = c("black", "blue", "red")) +
scale_y_continuous(limits = c(0, 25)) +
facet_grid(Month~.) +
ggtitle("Daily wind intensity by month in NYC") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.ticks = element_line())
```
It has an optional `label_fun` parameter that allows to modify the label aesthetics (such as rounding, adding percentage or currency suffixes and prefixes...).
```{r example2}
library(ggplot2)
library(ggspark)
ggplot(airquality, aes(Day, Wind, group = Month)) +
stat_interquartilerange(geom = "ribbon",
show.legend = FALSE) +
geom_line() +
stat_sparklabels(geom = "label", label_fun = \(x) round(x, 0),
show.legend = FALSE) +
scale_colour_manual("", values = c("black", "blue", "red")) +
scale_y_continuous(limits = c(0, 25)) +
facet_grid(Month~.) +
ggtitle("Daily wind intensity by month in NYC") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.ticks = element_line())
```
It is also possible to use points instead of labels.
```{r example3}
library(ggrepel)
ggplot(airquality, aes(Day, Wind, group = Month)) +
stat_interquartilerange(geom = "ribbon",
show.legend = FALSE) +
geom_line() +
stat_sparklabels(geom = "point",
show.legend = FALSE) +
scale_colour_manual("", values = c("black", "blue", "red")) +
scale_y_continuous(limits = c(0, 25)) +
facet_grid(Month~.) +
ggtitle("Daily wind intensity by month in NYC") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.ticks = element_line())
```
With ggrepel, it is possible to combine both text and dots.
```{r example4}
library(ggrepel)
ggplot(airquality, aes(Day, Wind, group = Month)) +
stat_interquartilerange(geom = "ribbon",
show.legend = FALSE) +
geom_line() +
stat_sparklabels(geom = "point", label_fun = \(x) round(x, 0),
show.legend = FALSE) +
stat_sparklabels(geom = "text_repel", label_fun = \(x) round(x, 0),
show.legend = FALSE) +
scale_colour_manual("", values = c("black", "blue", "red")) +
scale_y_continuous(limits = c(0, 25)) +
facet_grid(Month~.) +
ggtitle("Daily wind intensity by month in NYC") +
theme_minimal() +
theme(panel.grid = element_blank(),
axis.ticks = element_line())
```