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
- Rotate axis labels: We can do this by components of the `theme()`, specifically the `axis.text.x` component. Applying some vertical and horizontal justification to the labels centers them at the axis ticks. The `angle` can be set as desired within the 0 to 360 degree range, here we set it to 90 degrees.
48
50
49
-
```{r msleep-order-sleep-total-rotate}
51
+
```{r}
52
+
#| label: msleep-order-sleep-total-rotate
50
53
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
51
54
#| taxonomical orders of mammals on the x-axis. The x-axis labels are oriented
- Dodge axis labels: Add a `scale_*()` layer, e.g. `scale_x_continuous()`, `scale_y_discrete()`, etc., and customise the `guide` argument with the `guide_axis()` function. In this case we want to customise the x-axis, and the variable on the x-axis is discrete, so we'll use `scale_x_continuous()`. In the `guide` argument we use the `guide_axis()` and specify how many rows to dodge the labels into with `n.dodge`. This is likely a trial-and-error exercise, depending on the lengths of your labels and the width of your plot. In this case we've settled on 3 rows to render the labels.
69
73
70
-
```{r msleep-order-sleep-total-dodge}
74
+
```{r}
75
+
#| label: msleep-order-sleep-total-dodge
71
76
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
72
77
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
73
78
#| x-axis are dodged to three levels so that they remain readable."
- Omit overlapping labels: Alternatively, you can set `guide_axis(check.overlap = TRUE)` to omit axis labels that overlap. ggplot2 will prioritize the first, last, and middle labels. Note that this option might be more preferable for axes representing variables that have an inherent ordering that is obvious to the audience of the plot, so that it's trivial to guess what the missing labels are. (This is not the case for the following plot.)
80
85
81
-
```{r msleep-order-sleep-total-check-overlap}
86
+
```{r}
87
+
#| label: msleep-order-sleep-total-check-overlap
82
88
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
83
89
#| taxonomical orders of mammals on the x-axis. Several of the x-axis labels
84
90
#| have been omitted, but the one that remain are readable and don't overlap."
@@ -99,7 +105,8 @@ Add a `theme()` layer and set relevant arguments, e.g. `axis.title.x`, `axis.tex
99
105
100
106
Suppose we want to remove the axis labels entirely.
101
107
102
-
```{r ref.label="msleep-order-sleep-total"}
108
+
```{r}
109
+
#| ref-label: msleep-order-sleep-total
103
110
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
104
111
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
105
112
#| x-axis for the orders overlap and are unreadable."
Copy file name to clipboardExpand all lines: vignettes/articles/faq-faceting.Rmd
+6-3Lines changed: 6 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,8 @@ title: "FAQ: Faceting"
13
13
}
14
14
</style>
15
15
```
16
-
```{r, include = FALSE}
16
+
```{r}
17
+
#| include: false
17
18
library(ggplot2)
18
19
knitr::opts_chunk$set(
19
20
fig.dpi = 300,
@@ -77,7 +78,8 @@ In `facet_grid()` these values are determined by the number of levels of the var
77
78
Similarly, you can also use `facet_grid()` to facet by a single categorical variable as well.
78
79
In the formula notation, you use a `.` to indicate that no faceting should be done along that axis, i.e. `cyl ~ .` facets across the y-axis (within a column) while `. ~ cyl` facets across the x-axis (within a row).
79
80
80
-
```{r out.width = "50%"}
81
+
```{r}
82
+
#| out-width: 50%
81
83
#| fig.alt:
82
84
#| - "A histogram showing the city miles per gallon distribution. The plot has
83
85
#| four panels in a 4-row, 1-column layout, showing four numbers of cylinders."
@@ -303,7 +305,8 @@ df
303
305
304
306
You can plot `price` versus `time` and facet by `country`, but the resulting plot can be a bit difficult to read due to the shared y-axis label.
305
307
306
-
```{r warning = FALSE}
308
+
```{r}
309
+
#| warning: false
307
310
#| fig.alt: "A timeseries plot showing price over time for two countries, Japan
308
311
#| and the US, in two panels in a 2-row, 1-column layout. The countries are
309
312
#| indicated at the top of each panel. The two y-axes have different ranges."
@@ -28,7 +29,8 @@ It's strange to say, but this is a case where inventing a new OO system was actu
28
29
29
30
Here's a quick demo of ggproto in action:
30
31
31
-
```{r ggproto-intro}
32
+
```{r}
33
+
#| label: ggproto-intro
32
34
A <- ggproto("A", NULL,
33
35
x = 1,
34
36
inc = function(self) {
@@ -53,7 +55,8 @@ To create a new geom or stat, you will just create a new ggproto that inherits f
53
55
54
56
We'll start by creating a very simple stat: one that gives the convex hull (the _c_ hull) of a set of points. First we create a new ggproto object that inherits from `Stat`:
55
57
56
-
```{r chull}
58
+
```{r}
59
+
#| label: chull
57
60
StatChull <- ggproto("StatChull", Stat,
58
61
compute_group = function(data, scales) {
59
62
data[chull(data$x, data$y), , drop = FALSE]
@@ -374,7 +377,8 @@ It's harder to create a new geom than a new stat because you also need to know s
374
377
375
378
It's easiest to start with a simple example. The code below is a simplified version of `geom_point()`:
376
379
377
-
```{r GeomSimplePoint}
380
+
```{r}
381
+
#| label: GeomSimplePoint
378
382
#| fig.alt: "Scatterplot of engine displacement versus highway miles per
379
383
#| gallon, for 234 cars. The points are larger than the default."
@@ -625,7 +629,8 @@ title | `element_text()` | all text in title elements (plot, axes & lege
625
629
626
630
These set default properties that are inherited by more specific settings. These are most useful for setting an overall "background" colour and overall font settings (e.g. family and size).
627
631
628
-
```{r axis-line-ex}
632
+
```{r}
633
+
#| label: axis-line-ex
629
634
#| fig.alt:
630
635
#| - "Scatterplot of three observations arranged diagonally. The axis titles 'x'
631
636
#| and 'y' are coloured in black"
@@ -1150,7 +1155,8 @@ guide_key <- function(
1150
1155
1151
1156
Our new guide can now be used inside the `guides()` function or as the `guide` argument in a position scale.
1152
1157
1153
-
```{r key_example}
1158
+
```{r}
1159
+
#| label: key_example
1154
1160
#| fig.alt: >
1155
1161
#| Scatterplot of engine displacement versus highway miles per
1156
1162
#| gallon. The x-axis axis ticks are at 2.5, 3.5, 4.5, 5.5 and 6.5.
@@ -1174,7 +1180,8 @@ We'll edit the method so that the labels are drawn with a `colour` set in the ke
1174
1180
In addition to the `key` and `params` variable we've seen before, we now also have an `elements` variable, which is a list of precomputed theme elements. We can use the `elements$text` element to draw a graphical object (grob) in the style of axis text.
1175
1181
Perhaps the most finicky thing about drawing guides is that a lot of settings depend on the guide's `position` parameter.
1176
1182
1177
-
```{r key_ggproto_edit}
1183
+
```{r}
1184
+
#| label: key_ggproto_edit
1178
1185
# Same as before
1179
1186
GuideKey <- ggproto(
1180
1187
"Guide", GuideAxis,
@@ -1205,7 +1212,8 @@ GuideKey <- ggproto(
1205
1212
Because we are incorporating the `...` argument to `guide_key()` in the key, adding a `colour` column to the key is straightforward.
1206
1213
We can check that are guide looks correct in the different positions around the panel.
1207
1214
1208
-
```{r key_example_2}
1215
+
```{r}
1216
+
#| label: key_example_2
1209
1217
#| fig.alt: >
1210
1218
#| Scatterplot of engine displacement versus highway miles per
1211
1219
#| gallon. There are two x-axes at the bottom and top of the plot. The bottom
If the column name or expression is supplied by the user, you can also pass it to `aes()` or `vars()` using `{{ col }}`. This tidy eval operator captures the expression supplied by the user and forwards it to another tidy eval-enabled function such as `aes()` or `vars()`.
Generally, if you add a method for a ggplot2 generic like `autoplot()`, ggplot2 should be in `Imports`. If for some reason you would like to keep ggplot2 in `Suggests`, it is possible to register your generics only if ggplot2 is installed using `vctrs::s3_register()`. If you do this, you should copy and paste the source of `vctrs::s3_register()` into your own package to avoid adding a [vctrs](https://vctrs.r-lib.org/) dependency.
234
239
235
-
```{r, eval=FALSE}
240
+
```{r}
241
+
#| eval: false
236
242
.onLoad <- function(...) {
237
243
if (requireNamespace("ggplot2", quietly = TRUE)) {
0 commit comments