Skip to content

Commit c3083c7

Browse files
authored
Use knitr::convert_chunk_header(type = "yaml") (#6149)
1 parent 1dc053f commit c3083c7

11 files changed

+103
-46
lines changed

vignettes/articles/faq-annotation.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Annotation"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
library(dplyr)
1920
knitr::opts_chunk$set(

vignettes/articles/faq-axes.Rmd

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Axes"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
knitr::opts_chunk$set(
1920
fig.dpi = 300,
@@ -36,7 +37,8 @@ Set the angle of the text in the `axis.text.x` or `axis.text.y` components of th
3637

3738
In the following plot the labels on the x-axis are overlapping.
3839

39-
```{r msleep-order-sleep-total}
40+
```{r}
41+
#| label: msleep-order-sleep-total
4042
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
4143
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
4244
#| x-axis for the orders overlap and are unreadable."
@@ -46,7 +48,8 @@ ggplot(msleep, aes(x = order, y = sleep_total)) +
4648

4749
- 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.
4850

49-
```{r msleep-order-sleep-total-rotate}
51+
```{r}
52+
#| label: msleep-order-sleep-total-rotate
5053
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
5154
#| taxonomical orders of mammals on the x-axis. The x-axis labels are oriented
5255
#| vertically and are readable."
@@ -57,7 +60,8 @@ ggplot(msleep, aes(x = order, y = sleep_total)) +
5760

5861
- Flip the axes: Use the y-axis for long labels.
5962

60-
```{r msleep-order-sleep-total-flip}
63+
```{r}
64+
#| label: msleep-order-sleep-total-flip
6165
#| fig.alt: "A boxplot showing the total amount of sleep on the x-axis for 19
6266
#| taxonomical orders of mammals on the y-axis. The y-axis labels are oriented
6367
#| horizontally and are readable."
@@ -67,7 +71,8 @@ ggplot(msleep, aes(y = order, x = sleep_total)) +
6771

6872
- 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.
6973

70-
```{r msleep-order-sleep-total-dodge}
74+
```{r}
75+
#| label: msleep-order-sleep-total-dodge
7176
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
7277
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
7378
#| x-axis are dodged to three levels so that they remain readable."
@@ -78,7 +83,8 @@ ggplot(msleep, aes(x = order, y = sleep_total)) +
7883

7984
- 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.)
8085

81-
```{r msleep-order-sleep-total-check-overlap}
86+
```{r}
87+
#| label: msleep-order-sleep-total-check-overlap
8288
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
8389
#| taxonomical orders of mammals on the x-axis. Several of the x-axis labels
8490
#| 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
99105

100106
Suppose we want to remove the axis labels entirely.
101107

102-
```{r ref.label="msleep-order-sleep-total"}
108+
```{r}
109+
#| ref-label: msleep-order-sleep-total
103110
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
104111
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
105112
#| x-axis for the orders overlap and are unreadable."

vignettes/articles/faq-bars.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Barplots"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
library(dplyr)
1920
library(tidyr)

vignettes/articles/faq-customising.Rmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Customising"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
library(tibble)
1920
knitr::opts_chunk$set(
@@ -370,7 +371,8 @@ ggplot(mpg, aes(x = hwy, y = cty, color = class)) +
370371
If you would like all plots within a session/document to use a particular base size, you can set it with `set_theme()`.
371372
Run the following at the beginning of your session or include on top of your R Markdown document.
372373

373-
```{r eval = FALSE}
374+
```{r}
375+
#| eval: false
374376
set_theme(theme_gray(base_size = 18))
375377
```
376378

vignettes/articles/faq-faceting.Rmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Faceting"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
knitr::opts_chunk$set(
1920
fig.dpi = 300,
@@ -77,7 +78,8 @@ In `facet_grid()` these values are determined by the number of levels of the var
7778
Similarly, you can also use `facet_grid()` to facet by a single categorical variable as well.
7879
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).
7980

80-
```{r out.width = "50%"}
81+
```{r}
82+
#| out-width: 50%
8183
#| fig.alt:
8284
#| - "A histogram showing the city miles per gallon distribution. The plot has
8385
#| four panels in a 4-row, 1-column layout, showing four numbers of cylinders."
@@ -303,7 +305,8 @@ df
303305

304306
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.
305307

306-
```{r warning = FALSE}
308+
```{r}
309+
#| warning: false
307310
#| fig.alt: "A timeseries plot showing price over time for two countries, Japan
308311
#| and the US, in two panels in a 2-row, 1-column layout. The countries are
309312
#| indicated at the top of each panel. The two y-axes have different ranges."

vignettes/articles/faq-reordering.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ title: "FAQ: Reordering"
1313
}
1414
</style>
1515
```
16-
```{r, include = FALSE}
16+
```{r}
17+
#| include: false
1718
library(ggplot2)
1819
library(dplyr)
1920
library(tibble)

vignettes/extending-ggplot2.Rmd

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ vignette: >
99
%\VignetteEncoding{UTF-8}
1010
---
1111

12-
```{r, include = FALSE}
12+
```{r}
13+
#| include: false
1314
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 7, fig.align = "center")
1415
library(ggplot2)
1516
```
@@ -28,7 +29,8 @@ It's strange to say, but this is a case where inventing a new OO system was actu
2829

2930
Here's a quick demo of ggproto in action:
3031

31-
```{r ggproto-intro}
32+
```{r}
33+
#| label: ggproto-intro
3234
A <- ggproto("A", NULL,
3335
x = 1,
3436
inc = function(self) {
@@ -53,7 +55,8 @@ To create a new geom or stat, you will just create a new ggproto that inherits f
5355

5456
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`:
5557

56-
```{r chull}
58+
```{r}
59+
#| label: chull
5760
StatChull <- ggproto("StatChull", Stat,
5861
compute_group = function(data, scales) {
5962
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
374377

375378
It's easiest to start with a simple example. The code below is a simplified version of `geom_point()`:
376379

377-
```{r GeomSimplePoint}
380+
```{r}
381+
#| label: GeomSimplePoint
378382
#| fig.alt: "Scatterplot of engine displacement versus highway miles per
379383
#| gallon, for 234 cars. The points are larger than the default."
380384
GeomSimplePoint <- ggproto("GeomSimplePoint", Geom,
@@ -625,7 +629,8 @@ title | `element_text()` | all text in title elements (plot, axes & lege
625629

626630
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).
627631

628-
```{r axis-line-ex}
632+
```{r}
633+
#| label: axis-line-ex
629634
#| fig.alt:
630635
#| - "Scatterplot of three observations arranged diagonally. The axis titles 'x'
631636
#| and 'y' are coloured in black"
@@ -1150,7 +1155,8 @@ guide_key <- function(
11501155

11511156
Our new guide can now be used inside the `guides()` function or as the `guide` argument in a position scale.
11521157

1153-
```{r key_example}
1158+
```{r}
1159+
#| label: key_example
11541160
#| fig.alt: >
11551161
#| Scatterplot of engine displacement versus highway miles per
11561162
#| 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
11741180
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.
11751181
Perhaps the most finicky thing about drawing guides is that a lot of settings depend on the guide's `position` parameter.
11761182

1177-
```{r key_ggproto_edit}
1183+
```{r}
1184+
#| label: key_ggproto_edit
11781185
# Same as before
11791186
GuideKey <- ggproto(
11801187
"Guide", GuideAxis,
@@ -1205,7 +1212,8 @@ GuideKey <- ggproto(
12051212
Because we are incorporating the `...` argument to `guide_key()` in the key, adding a `colour` column to the key is straightforward.
12061213
We can check that are guide looks correct in the different positions around the panel.
12071214

1208-
```{r key_example_2}
1215+
```{r}
1216+
#| label: key_example_2
12091217
#| fig.alt: >
12101218
#| Scatterplot of engine displacement versus highway miles per
12111219
#| gallon. There are two x-axes at the bottom and top of the plot. The bottom

vignettes/ggplot2-in-packages.Rmd

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ vignette: >
99
%\VignetteEncoding{UTF-8}
1010
---
1111

12-
```{r, include = FALSE}
12+
```{r}
13+
#| include: false
1314
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.show = "hide")
1415
library(ggplot2)
1516
```
@@ -28,7 +29,8 @@ mpg_drv_summary <- function() {
2829
}
2930
```
3031

31-
```{r, include=FALSE}
32+
```{r}
33+
#| include: false
3234
# make sure this function runs!
3335
mpg_drv_summary()
3436
```
@@ -44,7 +46,8 @@ mpg_drv_summary <- function() {
4446
}
4547
```
4648

47-
```{r, include=FALSE}
49+
```{r}
50+
#| include: false
4851
# make sure this function runs!
4952
mpg_drv_summary()
5053
```
@@ -100,7 +103,8 @@ col_summary(mpg, "drv", "year")
100103

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

103-
```{r, eval = (packageVersion("rlang") >= "0.3.4.9003")}
106+
```{r}
107+
#| eval: !expr (packageVersion("rlang") >= "0.3.4.9003")
104108
col_summary <- function(df, col, by) {
105109
ggplot(df) +
106110
geom_bar(aes(y = {{ col }})) +
@@ -225,14 +229,16 @@ theme_custom <- function(...) {
225229
}
226230
```
227231

228-
```{r, include=FALSE}
232+
```{r}
233+
#| include: false
229234
# make sure this function runs!
230235
mpg_drv_summary() + theme_custom()
231236
```
232237

233238
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.
234239

235-
```{r, eval=FALSE}
240+
```{r}
241+
#| eval: false
236242
.onLoad <- function(...) {
237243
if (requireNamespace("ggplot2", quietly = TRUE)) {
238244
vctrs::s3_register("ggplot2::autoplot", "discrete_distr")

vignettes/ggplot2-specs.Rmd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ vignette: >
99
%\VignetteEncoding{UTF-8}
1010
---
1111

12-
```{r, include = FALSE}
12+
```{r}
13+
#| include: false
1314
library(ggplot2)
1415
knitr::opts_chunk$set(fig.dpi = 96, collapse = TRUE, comment = "#>")
1516
```
@@ -106,7 +107,9 @@ with this mistake.
106107
* The appearance of the line end is controlled by the `lineend` paramter,
107108
and can be one of "round", "butt" (the default), or "square".
108109
109-
```{r, out.width = "30%", fig.show = "hold"}
110+
```{r}
111+
#| out-width: 30%
112+
#| fig-show: hold
110113
#| fig.alt:
111114
#| - "A plot showing a line with an angle. A thinner red line is placed over
112115
#| a thicker black line. The black line ends where the red line ends."
@@ -134,7 +137,9 @@ with this mistake.
134137
* The appearance of line joins is controlled by `linejoin` and can be one of
135138
"round" (the default), "mitre", or "bevel".
136139
137-
```{r, out.width = "30%", fig.show = "hold"}
140+
```{r}
141+
#| out-width: 30%
142+
#| fig-show: hold
138143
#| fig.alt:
139144
#| - "A plot showing a thin red line on top of a thick black line shaped like
140145
#| the letter 'V'. The corner in the black V-shape is rounded."
@@ -192,7 +197,10 @@ Shapes take five types of values:
192197
193198
* The __name__ of the shape:
194199
195-
```{r out.width = "90%", fig.asp = 0.4, fig.width = 8}
200+
```{r}
201+
#| out-width: 90%
202+
#| fig-asp: 0.4
203+
#| fig-width: 8
196204
#| fig.alt: "An irregular 6-by-7 grid of point symbols annotated by the
197205
#| names that can be used to represent the symbols. Broadly, from top to
198206
#| bottom, the symbols are circles, squares, diamonds, triangles and

0 commit comments

Comments
 (0)