Skip to content

Commit d6d0523

Browse files
Alt text for documentation (#5226)
* Alt text for README * update extending-ggplot2 * update ggplot2-specs * update faq-annotation * update faq-axes * update faq-bars * update customising * update faceting * update reordering * Fix typos, don't recommend ..dot.. notation * Apply suggestions from code review Co-authored-by: Mara Averick <[email protected]> --------- Co-authored-by: Mara Averick <[email protected]>
1 parent 1f2a2ef commit d6d0523

10 files changed

+551
-13
lines changed

README.Rmd

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pak::pak("tidyverse/ggplot2")
4747
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()`).
4848

4949
```{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
52+
#| per gallon are inversely correlated."
5053
library(ggplot2)
5154
5255
ggplot(mpg, aes(displ, hwy, colour = class)) +

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ggplot(mpg, aes(displ, hwy, colour = class)) +
5353
geom_point()
5454
```
5555

56-
![](man/figures/README-example-1.png)<!-- -->
56+
<img src="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." />
5757

5858
## Lifecycle
5959

vignettes/articles/faq-annotation.Rmd

+34-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ You should use `annotate(geom = "text")` instead of `geom_text()` for annotation
3939
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.
4040

4141
```{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."
4245
mean_hwy <- round(mean(mpg$hwy), 2)
4346
4447
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
5962

6063

6164
```{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."
6268
ggplot(mpg, aes(x = hwy)) +
6369
geom_histogram(binwidth = 2) +
6470
annotate("segment",
@@ -85,6 +91,9 @@ Set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
8591
Suppose you have the following data frame and visualization. The labels at the edges of the plot are cut off slightly.
8692

8793
```{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."
8897
df <- tibble::tribble(
8998
~x, ~y, ~name,
9099
2, 2, "two",
@@ -99,6 +108,10 @@ ggplot(df, aes(x = x, y = y, label = name)) +
99108
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()`.
100109

101110
```{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
114+
#| entirety."
102115
ggplot(df, aes(x = x, y = y, label = name)) +
103116
geom_text(size = 10, vjust = "inward", hjust = "inward")
104117
```
@@ -107,7 +120,7 @@ ggplot(df, aes(x = x, y = y, label = name)) +
107120

108121
### How can I annotate my bar plot to display counts for each bar?
109122

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"`.
111124

112125
<details>
113126

@@ -116,6 +129,8 @@ Either calculate the counts ahead of time and place them on bars using `geom_tex
116129
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.
117130

118131
```{r}
132+
#| fig.alt = "A bar chart showing the number of cars for each of three types
133+
#| of drive train."
119134
ggplot(mpg, aes(x = drv)) +
120135
geom_bar()
121136
```
@@ -124,6 +139,8 @@ One option is to calculate the counts with `dplyr::count()` and then pass them t
124139
Note that we expanded the y axis limit to get the numbers to fit on the plot.
125140

126141
```{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."
127144
mpg %>%
128145
dplyr::count(drv) %>%
129146
ggplot(aes(x = drv, y = n)) +
@@ -132,9 +149,11 @@ mpg %>%
132149
coord_cartesian(ylim = c(0, 110))
133150
```
134151

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()`.
136153

137154
```{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."
138157
ggplot(mpg, aes(x = drv)) +
139158
geom_bar() +
140159
stat_count(geom = "text", aes(label = ..count..), vjust = -0.5) +
@@ -154,6 +173,9 @@ First calculate the counts for each segment (e.g. with `dplyr::count()`) and the
154173
Suppose you have the following stacked bar plot.
155174

156175
```{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."
157179
ggplot(mpg, aes(x = class, fill = drv)) +
158180
geom_bar()
159181
```
@@ -168,6 +190,10 @@ mpg %>%
168190
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()`.
169191

170192
```{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."
171197
mpg %>%
172198
count(class, drv) %>%
173199
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
188214
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.
189215

190216
```{r}
217+
#| fig.alt = "A bar chart showing the number of cars for each of three types
218+
#| of drive train."
191219
ggplot(mpg, aes(x = drv)) +
192220
geom_bar()
193221
```
194222

195223
One option is to calculate the proportions with `dplyr::count()` and then use `geom_col()` to draw the bars
196224

197225
```{r}
226+
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
227+
#| of drive train."
198228
mpg %>%
199229
dplyr::count(drv) %>%
200230
mutate(prop = n / sum(n)) %>%
@@ -206,6 +236,8 @@ Another option is to let `ggplot()` do the calculation of proportions for you, a
206236
Note that we also need to the `group = 1` mapping for this option.
207237

208238
```{r}
239+
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
240+
#| of drive train."
209241
ggplot(mpg, aes(x = drv, y = ..prop.., group = 1)) +
210242
geom_bar()
211243
```

0 commit comments

Comments
 (0)