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
+1-1
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ 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
50
+
#| fig.alt: "Scatterplot of engine displacement versus highway miles per
51
51
#| gallon, for 234 cars coloured by 7 'types' of car. The displacement and miles
Copy file name to clipboardExpand all lines: vignettes/articles/faq-annotation.Rmd
+12-12
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ 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
42
+
#| fig.alt: "Histogram of highway miles per gallon for 234 cars. A red line is
43
43
#| placed at the position 23.44 and is adorned with the label 'mean 23.44'.
44
44
#| Both the line and the text appear pixellated due to overplotting."
45
45
mean_hwy <- round(mean(mpg$hwy), 2)
@@ -62,7 +62,7 @@ This is because `geom_text()` draws the geom once per each row of the data frame
62
62
63
63
64
64
```{r}
65
-
#| fig.alt = "Histogram of highway miles per gallon for 234 cars. A red line is
65
+
#| fig.alt: "Histogram of highway miles per gallon for 234 cars. A red line is
66
66
#| placed at the position 23.44 and is adorned with the label 'mean = 23.44'.
67
67
#| Both the line and the text appear crisp."
68
68
ggplot(mpg, aes(x = hwy)) +
@@ -91,7 +91,7 @@ Set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
91
91
Suppose you have the following data frame and visualization. The labels at the edges of the plot are cut off slightly.
92
92
93
93
```{r}
94
-
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
94
+
#| fig.alt: "A plot showing the words 'two', 'three' and 'four' arranged
95
95
#| diagonally. The 'two' and 'four' labels have been clipped to the panel's
96
96
#| edge and are not displayed completely."
97
97
df <- tibble::tribble(
@@ -108,7 +108,7 @@ ggplot(df, aes(x = x, y = y, label = name)) +
108
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()`.
109
109
110
110
```{r}
111
-
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
111
+
#| fig.alt: "A plot showing the words 'two', 'three' and 'four' arranged
112
112
#| diagonally. The 'two' and 'four' labels are aligned to the top-right and
113
113
#| bottom-left relative to their anchor points, and are displayed in their
114
114
#| entirety."
@@ -129,7 +129,7 @@ Either calculate the counts ahead of time and place them on bars using `geom_tex
129
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.
130
130
131
131
```{r}
132
-
#| fig.alt = "A bar chart showing the number of cars for each of three types
132
+
#| fig.alt: "A bar chart showing the number of cars for each of three types
133
133
#| of drive train."
134
134
ggplot(mpg, aes(x = drv)) +
135
135
geom_bar()
@@ -139,7 +139,7 @@ One option is to calculate the counts with `dplyr::count()` and then pass them t
139
139
Note that we expanded the y axis limit to get the numbers to fit on the plot.
140
140
141
141
```{r}
142
-
#| fig.alt = "A bar chart showing the number of cars for each of three types
142
+
#| fig.alt: "A bar chart showing the number of cars for each of three types
143
143
#| of drive train. The count values are displayed on top of the bars as text."
144
144
mpg %>%
145
145
dplyr::count(drv) %>%
@@ -152,7 +152,7 @@ mpg %>%
152
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()`.
153
153
154
154
```{r}
155
-
#| fig.alt = "A bar chart showing the number of cars for each of three types
155
+
#| fig.alt: "A bar chart showing the number of cars for each of three types
156
156
#| of drive train. The count values are displayed on top of the bars as text."
157
157
ggplot(mpg, aes(x = drv)) +
158
158
geom_bar() +
@@ -173,7 +173,7 @@ First calculate the counts for each segment (e.g. with `dplyr::count()`) and the
173
173
Suppose you have the following stacked bar plot.
174
174
175
175
```{r}
176
-
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
176
+
#| fig.alt: "A stacked bar chart showing the number of cars for each of seven
177
177
#| types of cars. The fill colour of the bars indicate the type of drive
178
178
#| train."
179
179
ggplot(mpg, aes(x = class, fill = drv)) +
@@ -190,7 +190,7 @@ mpg %>%
190
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()`.
191
191
192
192
```{r}
193
-
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
193
+
#| fig.alt: "A stacked bar chart showing the number of cars for each of seven
194
194
#| types of cars. The fill colour of the bars indicate the type of drive
195
195
#| train. In the middle of each filled part, the count value is displayed as
196
196
#| text."
@@ -214,7 +214,7 @@ Either calculate the proportions ahead of time and place them on bars using `geo
214
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.
215
215
216
216
```{r}
217
-
#| fig.alt = "A bar chart showing the number of cars for each of three types
217
+
#| fig.alt: "A bar chart showing the number of cars for each of three types
218
218
#| of drive train."
219
219
ggplot(mpg, aes(x = drv)) +
220
220
geom_bar()
@@ -223,7 +223,7 @@ ggplot(mpg, aes(x = drv)) +
223
223
One option is to calculate the proportions with `dplyr::count()` and then use `geom_col()` to draw the bars
224
224
225
225
```{r}
226
-
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
226
+
#| fig.alt: "A bar chart showing the proportion of cars for each of three types
227
227
#| of drive train."
228
228
mpg %>%
229
229
dplyr::count(drv) %>%
@@ -236,7 +236,7 @@ Another option is to let `ggplot()` do the calculation of proportions for you, a
236
236
Note that we also need to the `group = 1` mapping for this option.
237
237
238
238
```{r}
239
-
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
239
+
#| fig.alt: "A bar chart showing the proportion of cars for each of three types
240
240
#| of drive train."
241
241
ggplot(mpg, aes(x = drv, y = ..prop.., group = 1)) +
0 commit comments