Skip to content

Commit

Permalink
Use knitr::convert_chunk_header(type = "yaml")
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 21, 2024
1 parent ddd207e commit 12ad97d
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 46 deletions.
3 changes: 2 additions & 1 deletion vignettes/articles/faq-annotation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Annotation"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
library(dplyr)
knitr::opts_chunk$set(
Expand Down
21 changes: 14 additions & 7 deletions vignettes/articles/faq-axes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Axes"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
knitr::opts_chunk$set(
fig.dpi = 300,
Expand All @@ -36,7 +37,8 @@ Set the angle of the text in the `axis.text.x` or `axis.text.y` components of th

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

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

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

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

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

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

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

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

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

```{r msleep-order-sleep-total-check-overlap}
```{r}
#| label: msleep-order-sleep-total-check-overlap
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
#| taxonomical orders of mammals on the x-axis. Several of the x-axis labels
#| have been omitted, but the one that remain are readable and don't overlap."
Expand All @@ -99,7 +105,8 @@ Add a `theme()` layer and set relevant arguments, e.g. `axis.title.x`, `axis.tex

Suppose we want to remove the axis labels entirely.

```{r ref.label="msleep-order-sleep-total"}
```{r}
#| ref-label: msleep-order-sleep-total
#| fig.alt: "A boxplot showing the total amount of sleep on the y-axis for 19
#| taxonomical orders of mammals on the x-axis. The horizontal labels on the
#| x-axis for the orders overlap and are unreadable."
Expand Down
3 changes: 2 additions & 1 deletion vignettes/articles/faq-bars.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Barplots"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
library(dplyr)
library(tidyr)
Expand Down
6 changes: 4 additions & 2 deletions vignettes/articles/faq-customising.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Customising"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
library(tibble)
knitr::opts_chunk$set(
Expand Down Expand Up @@ -370,7 +371,8 @@ ggplot(mpg, aes(x = hwy, y = cty, color = class)) +
If you would like all plots within a session/document to use a particular base size, you can set it with `set_theme()`.
Run the following at the beginning of your session or include on top of your R Markdown document.

```{r eval = FALSE}
```{r}
#| eval: false
set_theme(theme_gray(base_size = 18))
```

Expand Down
9 changes: 6 additions & 3 deletions vignettes/articles/faq-faceting.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Faceting"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
knitr::opts_chunk$set(
fig.dpi = 300,
Expand Down Expand Up @@ -77,7 +78,8 @@ In `facet_grid()` these values are determined by the number of levels of the var
Similarly, you can also use `facet_grid()` to facet by a single categorical variable as well.
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).

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

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.

```{r warning = FALSE}
```{r}
#| warning: false
#| fig.alt: "A timeseries plot showing price over time for two countries, Japan
#| and the US, in two panels in a 2-row, 1-column layout. The countries are
#| indicated at the top of each panel. The two y-axes have different ranges."
Expand Down
3 changes: 2 additions & 1 deletion vignettes/articles/faq-reordering.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ title: "FAQ: Reordering"
}
</style>
```
```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
library(dplyr)
library(tibble)
Expand Down
24 changes: 16 additions & 8 deletions vignettes/extending-ggplot2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

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

Here's a quick demo of ggproto in action:

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

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

```{r chull}
```{r}
#| label: chull
StatChull <- ggproto("StatChull", Stat,
compute_group = function(data, scales) {
data[chull(data$x, data$y), , drop = FALSE]
Expand Down Expand Up @@ -374,7 +377,8 @@ It's harder to create a new geom than a new stat because you also need to know s

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

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

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

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

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

```{r key_example}
```{r}
#| label: key_example
#| fig.alt: >
#| Scatterplot of engine displacement versus highway miles per
#| gallon. The x-axis axis ticks are at 2.5, 3.5, 4.5, 5.5 and 6.5.
Expand All @@ -1174,7 +1180,8 @@ We'll edit the method so that the labels are drawn with a `colour` set in the ke
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.
Perhaps the most finicky thing about drawing guides is that a lot of settings depend on the guide's `position` parameter.

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

```{r key_example_2}
```{r}
#| label: key_example_2
#| fig.alt: >
#| Scatterplot of engine displacement versus highway miles per
#| gallon. There are two x-axes at the bottom and top of the plot. The bottom
Expand Down
18 changes: 12 additions & 6 deletions vignettes/ggplot2-in-packages.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
```{r}
#| include: false
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.show = "hide")
library(ggplot2)
```
Expand All @@ -28,7 +29,8 @@ mpg_drv_summary <- function() {
}
```

```{r, include=FALSE}
```{r}
#| include: false
# make sure this function runs!
mpg_drv_summary()
```
Expand All @@ -44,7 +46,8 @@ mpg_drv_summary <- function() {
}
```

```{r, include=FALSE}
```{r}
#| include: false
# make sure this function runs!
mpg_drv_summary()
```
Expand Down Expand Up @@ -100,7 +103,8 @@ col_summary(mpg, "drv", "year")

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

```{r, eval = (packageVersion("rlang") >= "0.3.4.9003")}
```{r}
#| eval: !expr (packageVersion("rlang") >= "0.3.4.9003")
col_summary <- function(df, col, by) {
ggplot(df) +
geom_bar(aes(y = {{ col }})) +
Expand Down Expand Up @@ -225,14 +229,16 @@ theme_custom <- function(...) {
}
```

```{r, include=FALSE}
```{r}
#| include: false
# make sure this function runs!
mpg_drv_summary() + theme_custom()
```

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.

```{r, eval=FALSE}
```{r}
#| eval: false
.onLoad <- function(...) {
if (requireNamespace("ggplot2", quietly = TRUE)) {
vctrs::s3_register("ggplot2::autoplot", "discrete_distr")
Expand Down
16 changes: 12 additions & 4 deletions vignettes/ggplot2-specs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
```{r}
#| include: false
library(ggplot2)
knitr::opts_chunk$set(fig.dpi = 96, collapse = TRUE, comment = "#>")
```
Expand Down Expand Up @@ -106,7 +107,9 @@ with this mistake.
* The appearance of the line end is controlled by the `lineend` paramter,
and can be one of "round", "butt" (the default), or "square".

```{r, out.width = "30%", fig.show = "hold"}
```{r}
#| out-width: 30%
#| fig-show: hold
#| fig.alt:
#| - "A plot showing a line with an angle. A thinner red line is placed over
#| a thicker black line. The black line ends where the red line ends."
Expand Down Expand Up @@ -134,7 +137,9 @@ with this mistake.
* The appearance of line joins is controlled by `linejoin` and can be one of
"round" (the default), "mitre", or "bevel".

```{r, out.width = "30%", fig.show = "hold"}
```{r}
#| out-width: 30%
#| fig-show: hold
#| fig.alt:
#| - "A plot showing a thin red line on top of a thick black line shaped like
#| the letter 'V'. The corner in the black V-shape is rounded."
Expand Down Expand Up @@ -192,7 +197,10 @@ Shapes take five types of values:

* The __name__ of the shape:

```{r out.width = "90%", fig.asp = 0.4, fig.width = 8}
```{r}
#| out-width: 90%
#| fig-asp: 0.4
#| fig-width: 8
#| fig.alt: "An irregular 6-by-7 grid of point symbols annotated by the
#| names that can be used to represent the symbols. Broadly, from top to
#| bottom, the symbols are circles, squares, diamonds, triangles and
Expand Down
Loading

0 comments on commit 12ad97d

Please sign in to comment.