Skip to content

Commit

Permalink
Translate c() to YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Sep 6, 2024
1 parent 030e403 commit 2233cfc
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 57 deletions.
18 changes: 8 additions & 10 deletions vignettes/articles/faq-bars.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ By default, the `width` of bars is `0.9` (90% of the resolution of the data).
You can set this argument to a lower value to get bars that are narrower with more space between them.

```{r}
#| fig.alt: c(
#| "A bar chart showing the number of cars for each of three types
#| of drive train. The bars are somewhat narrower than the default.",
#| "A bar chart showing the number of cars for each of three types
#| fig.alt:
#| - "A bar chart showing the number of cars for each of three types
#| of drive train. The bars are somewhat narrower than the default."
#| - "A bar chart showing the number of cars for each of three types
#| of drive train. The bars are very narrow."
#| )
ggplot(mpg, aes(x = drv)) +
geom_bar(width = 0.5)
Expand Down Expand Up @@ -417,14 +416,13 @@ ggplot(df, aes(x = x, y = y)) +
Also suppose that you want to cut off the bars at `y = 1000` since you know that the variable you're plotting cannot take a value less than 1000, you might use `geom_point()` instead.

```{r}
#| fig.alt: c(
#| "A bar chart showing numbers for 3 arbitrary categories. The y-axis starts
#| fig.alt:
#| - "A bar chart showing numbers for 3 arbitrary categories. The y-axis starts
#| at 1000 and the bars all look different in height. This is not a recommended
#| way of plotting this data.",
#| "A scatter plot with 3 points showing numbers for 3 arbitrary categories.
#| way of plotting this data."
#| - "A scatter plot with 3 points showing numbers for 3 arbitrary categories.
#| The y-axis starts at 1000 and the points have visually different values.
#| This is a better way of plotting this data."
#| )
# don't do this
ggplot(df, aes(x = x, y = y)) +
geom_col() +
Expand Down
10 changes: 5 additions & 5 deletions vignettes/articles/faq-customising.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ ggplot(mpg, aes(x = hwy, y = cty, color = drv)) +
If a legend is drawn for multiple aesthetics, you'll want to update the title for all of them.

```{r}
#| fig.alt: c(
#| "A scatter plot showing the highway miles per gallon on the x-axis
#| fig.alt:
#| - "A scatter plot showing the highway miles per gallon on the x-axis
#| and city miles per gallon on the y-axis. The point shapes and colours
#| indicate three types of drive train. The shapes and colours are displayed in
#| separate legends titled 'drv' and 'Drive train' respectively.",
#| "A scatter plot showing the highway miles per gallon on the x-axis
#| separate legends titled 'drv' and 'Drive train' respectively."
#| - "A scatter plot showing the highway miles per gallon on the x-axis
#| and city miles per gallon on the y-axis. The point shapes and colours
#| indicate three types of drive train. The shapes and colours are displayed in
#| a single legend titled 'Drive train'."
#| )
#|
# not this
ggplot(mpg, aes(x = hwy, y = cty, color = drv, shape = drv)) +
geom_point() +
Expand Down
7 changes: 3 additions & 4 deletions vignettes/articles/faq-faceting.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ Similarly, you can also use `facet_grid()` to facet by a single categorical vari
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%"}
#| fig.alt: c(
#| "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.",
#| 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."
#| "A histogram showing the city miles per gallon distribution. The plot has
#| four panels in a 1-row, 4-column layout, showing four numbers of cylinders."
#| )
ggplot(mpg, aes(x = cty)) +
geom_histogram() +
facet_grid(cyl ~ .)
Expand Down
9 changes: 4 additions & 5 deletions vignettes/articles/faq-reordering.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,15 @@ df_arranged %>%
If you wanted to make sure that the observation identified with an asterisk is always plotted on top, regardless of how the data are arranged in the data frame, you can create an additional layer for that observation.

```{r}
#| fig.alt: c(
#| "A scatter plot showing four points at the same y-positions but at four
#| fig.alt:
#| - "A scatter plot showing four points at the same y-positions but at four
#| x-positions, of which two are very distinct. Every point has a distinct shape
#| and colour. A yellow triangle is plotted on top of a blue circle. A black
#| asterisk is plotted on top of a red square.",
#| "A scatter plot showing four points at the same y-positions but at four
#| asterisk is plotted on top of a red square."
#| - "A scatter plot showing four points at the same y-positions but at four
#| x-positions, of which two are very distinct. Every point has a distinct shape
#| and colour. A blue circle is plotted on top of a yellow triangle. A black
#| asterisk is plotted on top of a red square."
#| )
ggplot(mapping = aes(x = x, y = y, fill = fill, shape = shape)) +
geom_point(data = df %>% filter(shape != "asterisk"), size = 8) +
Expand Down
34 changes: 15 additions & 19 deletions vignettes/extending-ggplot2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,12 @@ Sometimes you have calculations that should be performed once for the complete d
To do this we override the `setup_params()` method. It's passed the data and a list of params, and returns an updated list.

```{r}
#| fig.alt: c(
#| "A line plot showing three kernel density estimates of engine displacement,
#| fig.alt:
#| - "A line plot showing three kernel density estimates of engine displacement,
#| coloured for three types of drive trains. The lines are a little bit
#| wobbly.",
#| "A line plot showing three kernel density estimates of engine displacement,
#| wobbly."
#| - "A line plot showing three kernel density estimates of engine displacement,
#| coloured for three types of drive trains. The lines are fairly smooth."
#| )
StatDensityCommon <- ggproto("StatDensityCommon", Stat,
required_aes = "x",
Expand Down Expand Up @@ -310,16 +309,15 @@ ggplot(mpg, aes(displ, fill = drv)) +
This is because each density is computed independently, and the estimated `x`s don't line up. We can resolve that issue by computing the range of the data once in `setup_params()`.

```{r}
#| fig.alt: c(
#| "A stacked area plot showing kernel density estimates of engine displacement.
#| fig.alt:
#| - "A stacked area plot showing kernel density estimates of engine displacement.
#| Three areas are shown that indicate the estimates for three types of drive
#| trains separately. The areas are stacked on top of one another and show
#| no overlap.",
#| "A heatmap showing the density of engine displacement for three types of
#| no overlap."
#| - "A heatmap showing the density of engine displacement for three types of
#| drive trains. The heatmap has three rows for the drive trains, but are
#| continuous in the horizontal direction. The fill intensity of the heatmap
#| shows the kernel density estimates."
#| )
StatDensityCommon <- ggproto("StatDensityCommon", Stat,
required_aes = "x",
default_aes = aes(y = after_stat(density)),
Expand Down Expand Up @@ -628,12 +626,11 @@ 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}
#| fig.alt: c(
#| "Scatterplot of three observations arranged diagonally. The axis titles 'x'
#| and 'y' are coloured in black",
#| "Scatterplot of three observations arranged diagonally. The axis titles 'x'
#| fig.alt:
#| - "Scatterplot of three observations arranged diagonally. The axis titles 'x'
#| and 'y' are coloured in black"
#| - "Scatterplot of three observations arranged diagonally. The axis titles 'x'
#| and 'y' are coloured in red"
#| )
df <- data.frame(x = 1:3, y = 1:3)
base <- ggplot(df, aes(x, y)) +
geom_point() +
Expand Down Expand Up @@ -809,11 +806,10 @@ FacetDuplicate <- ggproto("FacetDuplicate", Facet,
Now with everything assembled, lets test it out:

```{r}
#| fig.alt: c(
#| "Scatterplot showing horsepower against miles per gallon for 32 cars.",
#| "Scatterplot with two panels showing horsepower against miles per gallon for
#| fig.alt:
#| - "Scatterplot showing horsepower against miles per gallon for 32 cars."
#| - "Scatterplot with two panels showing horsepower against miles per gallon for
#| 32 cars. The left and right panels are identical."
#| )
p <- ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point()
p
p + facet_duplicate()
Expand Down
26 changes: 12 additions & 14 deletions vignettes/ggplot2-specs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ with this mistake.
and can be one of "round", "butt" (the default), or "square".

```{r, out.width = "30%", fig.show = "hold"}
#| fig.alt: c(
#| "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.",
#| "A plot showing a line with an angle. A thinner red line is placed over
#| 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."
#| - "A plot showing a line with an angle. A thinner red line is placed over
#| a thicker black line. The black line ends past where the red line ends,
#| and ends in a semicircle.",
#| "A plot showing a line with an angle. A thinner red line is placed over
#| and ends in a semicircle."
#| - "A plot showing a line with an angle. A thinner red line is placed over
#| a thicker black line. The black line ends past where the red line ends,
#| and ends in a square shape."
#| )
df <- data.frame(x = 1:3, y = c(4, 1, 9))
base <- ggplot(df, aes(x, y)) + xlim(0.5, 3.5) + ylim(0, 10)
base +
Expand All @@ -136,15 +135,14 @@ with this mistake.
"round" (the default), "mitre", or "bevel".

```{r, out.width = "30%", fig.show = "hold"}
#| fig.alt: c(
#| "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.",
#| "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 sharp.",
#| "A plot showing a thin red line on top of a thick black line shaped like
#| 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."
#| - "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 sharp."
#| - "A plot showing a thin red line on top of a thick black line shaped like
#| the letter 'V'. A piece of the corner is cut off so that the two
#| straight parts are connected by a horizontal part."
#| )
df <- data.frame(x = 1:3, y = c(9, 1, 9))
base <- ggplot(df, aes(x, y)) + ylim(0, 10)
base +
Expand Down

0 comments on commit 2233cfc

Please sign in to comment.