You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.Rmd
+3
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,9 @@ pak::pak("tidyverse/ggplot2")
47
47
It's hard to succinctly describe how ggplot2 works because it embodies a deep philosophy of visualisation. However, in most cases you start with `ggplot()`, supply a dataset and aesthetic mapping (with `aes()`). You then add on layers (like `geom_point()` or `geom_histogram()`), scales (like `scale_colour_brewer()`), faceting specifications (like `facet_wrap()`) and coordinate systems (like `coord_flip()`).
48
48
49
49
```{r example}
50
+
#| fig.alt = "Scatterplot of engine displacement versus highway miles per
51
+
#| gallon, for 234 cars coloured by 7 'types' of car. The displacement and miles
<imgsrc="man/figures/README-example-1.png"alt="Scatterplot of engine displacement versus highway miles per gallon, for 234 cars coloured by 7 'types' of car. The displacement and miles per gallon are inversely correlated." />
Copy file name to clipboardExpand all lines: vignettes/articles/faq-annotation.Rmd
+34-2
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,9 @@ You should use `annotate(geom = "text")` instead of `geom_text()` for annotation
39
39
In the following visualisation we have annotated a histogram with a red line and red text to mark the mean. Note that both the line and the text appears pixellated/fuzzy.
40
40
41
41
```{r}
42
+
#| fig.alt = "Histogram of highway miles per gallon for 234 cars. A red line is
43
+
#| placed at the position 23.44 and is adorned with the label 'mean 23.44'.
44
+
#| Both the line and the text appear pixellated due to overplotting."
42
45
mean_hwy <- round(mean(mpg$hwy), 2)
43
46
44
47
ggplot(mpg, aes(x = hwy)) +
@@ -59,6 +62,9 @@ This is because `geom_text()` draws the geom once per each row of the data frame
59
62
60
63
61
64
```{r}
65
+
#| fig.alt = "Histogram of highway miles per gallon for 234 cars. A red line is
66
+
#| placed at the position 23.44 and is adorned with the label 'mean = 23.44'.
67
+
#| Both the line and the text appear crisp."
62
68
ggplot(mpg, aes(x = hwy)) +
63
69
geom_histogram(binwidth = 2) +
64
70
annotate("segment",
@@ -85,6 +91,9 @@ Set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
85
91
Suppose you have the following data frame and visualization. The labels at the edges of the plot are cut off slightly.
86
92
87
93
```{r}
94
+
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
95
+
#| diagonally. The 'two' and 'four' labels have been clipped to the panel's
96
+
#| edge and are not displayed completely."
88
97
df <- tibble::tribble(
89
98
~x, ~y, ~name,
90
99
2, 2, "two",
@@ -99,6 +108,10 @@ ggplot(df, aes(x = x, y = y, label = name)) +
99
108
You could manually extend axis limits to avoid this, but a more straightforward approach is to set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
100
109
101
110
```{r}
111
+
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
112
+
#| diagonally. The 'two' and 'four' labels are aligned to the top-right and
113
+
#| bottom-left relative to their anchor points, and are displayed in their
@@ -107,7 +120,7 @@ ggplot(df, aes(x = x, y = y, label = name)) +
107
120
108
121
### How can I annotate my bar plot to display counts for each bar?
109
122
110
-
Either calculate the counts ahead of time and place them on bars using `geom_text()` or let `ggplot()` calculate them for you and then add them to the plot using `stat_coun()` with `geom = "text"`.
123
+
Either calculate the counts ahead of time and place them on bars using `geom_text()` or let `ggplot()` calculate them for you and then add them to the plot using `stat_count()` with `geom = "text"`.
111
124
112
125
<details>
113
126
@@ -116,6 +129,8 @@ Either calculate the counts ahead of time and place them on bars using `geom_tex
116
129
Suppose you have the following bar plot and you want to add the number of cars that fall into each `drv` level on their respective bars.
117
130
118
131
```{r}
132
+
#| fig.alt = "A bar chart showing the number of cars for each of three types
133
+
#| of drive train."
119
134
ggplot(mpg, aes(x = drv)) +
120
135
geom_bar()
121
136
```
@@ -124,6 +139,8 @@ One option is to calculate the counts with `dplyr::count()` and then pass them t
124
139
Note that we expanded the y axis limit to get the numbers to fit on the plot.
125
140
126
141
```{r}
142
+
#| fig.alt = "A bar chart showing the number of cars for each of three types
143
+
#| of drive train. The count values are displayed on top of the bars as text."
127
144
mpg %>%
128
145
dplyr::count(drv) %>%
129
146
ggplot(aes(x = drv, y = n)) +
@@ -132,9 +149,11 @@ mpg %>%
132
149
coord_cartesian(ylim = c(0, 110))
133
150
```
134
151
135
-
Another option is to let `ggplot()` do the counting for you, and access these counts with `..count..` that is mapped to the labels to be placed on the plot with `stat_count()`.
152
+
Another option is to let `ggplot()` do the counting for you, and access these counts with `after_stat(count)` that is mapped to the labels to be placed on the plot with `stat_count()`.
136
153
137
154
```{r}
155
+
#| fig.alt = "A bar chart showing the number of cars for each of three types
156
+
#| of drive train. The count values are displayed on top of the bars as text."
@@ -154,6 +173,9 @@ First calculate the counts for each segment (e.g. with `dplyr::count()`) and the
154
173
Suppose you have the following stacked bar plot.
155
174
156
175
```{r}
176
+
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
177
+
#| types of cars. The fill colour of the bars indicate the type of drive
178
+
#| train."
157
179
ggplot(mpg, aes(x = class, fill = drv)) +
158
180
geom_bar()
159
181
```
@@ -168,6 +190,10 @@ mpg %>%
168
190
You can then pass this result directly to `ggplot()`, draw the segments with appropriate heights with `y = n` in the `aes`thetic mapping and `geom_col()` to draw the bars, and finally place the counts on the plot with `geom_text()`.
169
191
170
192
```{r}
193
+
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
194
+
#| types of cars. The fill colour of the bars indicate the type of drive
195
+
#| train. In the middle of each filled part, the count value is displayed as
196
+
#| text."
171
197
mpg %>%
172
198
count(class, drv) %>%
173
199
ggplot(aes(x = class, fill = drv, y = n)) +
@@ -188,13 +214,17 @@ Either calculate the prpportions ahead of time and place them on bars using `geo
188
214
Suppose you have the following bar plot but you want to display the proportion of cars that fall into each `drv` level, instead of the count.
189
215
190
216
```{r}
217
+
#| fig.alt = "A bar chart showing the number of cars for each of three types
218
+
#| of drive train."
191
219
ggplot(mpg, aes(x = drv)) +
192
220
geom_bar()
193
221
```
194
222
195
223
One option is to calculate the proportions with `dplyr::count()` and then use `geom_col()` to draw the bars
196
224
197
225
```{r}
226
+
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
227
+
#| of drive train."
198
228
mpg %>%
199
229
dplyr::count(drv) %>%
200
230
mutate(prop = n / sum(n)) %>%
@@ -206,6 +236,8 @@ Another option is to let `ggplot()` do the calculation of proportions for you, a
206
236
Note that we also need to the `group = 1` mapping for this option.
207
237
208
238
```{r}
239
+
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
240
+
#| of drive train."
209
241
ggplot(mpg, aes(x = drv, y = ..prop.., group = 1)) +
0 commit comments