Skip to content

Commit

Permalink
Tweak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhodge931 committed Nov 8, 2023
1 parent 45d3f50 commit b5d30f2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 90 deletions.
8 changes: 4 additions & 4 deletions vignettes/articles/3_go_further-Rmd.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ penguins2 |>
y = flipper_length_mm,
col = species,
position = "dodge",
width = 0.5,
# coord = coord_flip()
width = 0.75
) +
geom_text(
aes(
Expand All @@ -316,8 +315,9 @@ penguins2 |>
),
show.legend = FALSE,
size = 3.53,
position = position_dodge(width = 0.5)
)
position = position_dodge(width = 0.75)
) +
coord_flip()
```

```{r}
Expand Down
10 changes: 10 additions & 0 deletions vignettes/articles/4_extensions-Rmd.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ p3 <- mtcars |>
y = qsec,
x_breaks = scales::breaks_pretty(4)) +
facet_wrap(~"Plot 3")
(p1 + p2) / p3 +
plot_annotation(title = "A collection of plots patched together",
subtitle = "Works so nice!",
theme = light_mode() +
theme(
plot.title = element_text(margin = margin(t = 10 * -0.5, b = 10 * 0.75)),
plot.subtitle = element_text(margin = margin())
)
)
```

#### ggbeeswarm
Expand Down
180 changes: 94 additions & 86 deletions vignettes/ggblanket.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Secondary objectives relate to:
7. Ability to customise by prefixed arguments
8. Ability to customise the theme
9. Access to other geom\_\* arguments via ...
10. Ability to add multiple geom\_\* layers
11. Pretty continuous positional scales
10. Pretty continuous positional scales
11. Ability to add multiple geom\_\* layers
12. Other key differences to ggplot2

```{r setup}
Expand Down Expand Up @@ -342,7 +342,70 @@ penguins2 |>
level = 0.99) #accessed via geom_smooth
```

### 10. Ability to add multiple geom\_\* layers
### 10. Pretty continuous positional scales

Where the orientation is normal (i.e. vertical):

- Default x scale limits and expanding are as per ggplot2 defaults
- Default continuous y scale limits (that are non-transformed) start and end on a break with zero expanding
- Default categorical (or numeric transformed) y scale limits and expanding are as per ggplot2 defaults
- Default removal of vertical gridlines

It does the opposite where the orientation is horizontal.

Note this symmetry approach does _not_ apply:
* if a transformation other than identity or reverse is applied to x or y scales.
* for `gg_raster`, `gg_contour_filled` or `gg_density_2d_filled`

In some circumstances the ggplot2 approach to default scales may be preferable. In these cases, users can revert to the ggplot2 approach by using `*_limits = c(NA, NA)` and `*_expand = c(0.05, 0.05)` (or add `scale_*_continuous()`).

```{r, fig.asp=0.55}
# ggplot2
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
ggplot() +
geom_col(aes(x = body_mass_g,
y = species,
fill = sex),
position = "dodge",
width = 0.66)
```

```{r}
# ggblanket
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
gg_col(
x = body_mass_g,
y = species,
col = sex,
position = "dodge",
width = 0.66)
```

Sometimes with small plots or faceted plots etc, the labels can be too squashed. Making the breaks width bigger can waste space, due to the afore-mentioned approach of ggblanket to making pretty scales. An alternative approach is to use the `str_keep_seq` function with the `*_labels` arguments to only keep every 2nd (or nth) label.

```{r}
# ggblanket
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
ungroup() |>
gg_col(
y = body_mass_g,
x = species,
col = sex,
position = "dodge",
width = 0.5,
x_labels = \(x) stringr::str_sub(x, 1, 1),
y_labels = \(x) str_keep_seq(x),
title = "Keep every 2nd label",
theme = light_mode(title_face = "plain"))
```

### 11. Ability to add multiple geom\_\* layers

Users can make plots with multiple layers with ggblanket by adding on `ggplot2::geom_*` layers.

Expand Down Expand Up @@ -390,6 +453,8 @@ p1 + p2

3. In some situations, `gg_blank` may be required.

4. If users are building a horizontal plot that includes multiple geoms, it is recommended that users build the plot vertically with ggblanket - and then use `ggplot2::coord_flip` to make it horizontally.

Users need to ensure that the scales built by their `gg_*` function are appropriate for subsequent layers. Plot scales are built by the `gg_*` function based on the `data`, `x`, `y`, `*_limits`, `*_include`, `stat`, `position` and `coord` arguments in the `gg_*` function.

```{r, fig.asp=0.75}
Expand All @@ -401,98 +466,41 @@ d <- penguins2 |>
mutate(upper = body_mass_g * 1.2)
p1 <- d |>
gg_col(
y = species,
x = body_mass_g,
gg_col(
x = species,
y = body_mass_g,
col = species,
x_labels = \(x) x / 1000,
x_title = "Body mass kg",
x_include = max(d$upper),
width = 0.75,
col_legend_place = "n") +
geom_errorbar(aes(xmin = lower, xmax = upper, y = species),
inherit.aes = FALSE,
colour = "black",
width = 0.1)
y_include = c(0, max(d$upper)),
y_labels = \(x) x / 1000,
y_title = "Body mass kg",
col_legend_place = "b") +
geom_errorbar(aes(ymin = lower, ymax = upper),
colour = "black",
width = 0.1) +
coord_flip()
p1
p2 <- d |>
gg_blank(
y = species,
x = body_mass_g,
xmin = lower,
xmax = upper,
gg_col(
x = species,
y = body_mass_g,
col = species,
x_include = 0,
x_labels = \(x) x / 1000,
x_title = "Body mass kg",
col_legend_place = "n") +
geom_col(colour = NA, fill = "#d3d3d3", width = 0.75) +
geom_errorbar(width = 0.1)
colour = "#d3d3d3",
fill = "#d3d3d3",
width = 0.75,
y_include = c(0, max(d$upper)),
y_labels = \(x) x / 1000,
y_title = "Body mass kg",
col_legend_place = "b") +
geom_errorbar(aes(ymin = lower, ymax = upper),
width = 0.1) +
coord_flip()
p1 / p2
```

### 11. Pretty continuous positional scales

Where the orientation is normal (i.e. vertical):

- Default x scale limits and expanding are as per ggplot2 defaults
- Default continuous y scale limits (that are non-transformed) start and end on a break with zero expanding
- Default categorical (or numeric transformed) y scale limits and expanding are as per ggplot2 defaults
- Default removal of vertical gridlines

It does the opposite where the orientation is horizontal.
Note this symmetry approach does _not_ apply:
* if a transformation other than identity or reverse is applied to x or y scales.
* for `gg_raster`, `gg_contour_filled` or `gg_density_2d_filled`
In some circumstances the ggplot2 approach to default scales may be preferable. In these cases, users can revert to the ggplot2 approach by using `*_limits = c(NA, NA)` and `*_expand = c(0.05, 0.05)` (or add `scale_*_continuous()`).

```{r, fig.asp=0.55}
# ggplot2
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
ggplot() +
geom_col(aes(x = body_mass_g,
y = species,
fill = sex),
position = "dodge",
width = 0.66)
```

```{r}
# ggblanket
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
gg_col(
x = body_mass_g,
y = species,
col = sex,
position = "dodge",
width = 0.66)
```

Sometimes with small plots or faceted plots etc, the labels can be too squashed. Making the breaks width bigger can waste space, due to the afore-mentioned approach of ggblanket to making pretty scales. An alternative approach is to use the `str_keep_seq` function with the `*_labels` arguments to only keep every 2nd (or nth) label.

```{r}
# ggblanket
penguins2 |>
group_by(species, sex) |>
summarise(body_mass_g = mean(body_mass_g)) |>
ungroup() |>
gg_col(
y = body_mass_g,
x = species,
col = sex,
position = "dodge",
width = 0.5,
x_labels = \(x) stringr::str_sub(x, 1, 1),
y_labels = \(x) str_keep_seq(x),
title = "Keep every 2nd label",
theme = light_mode(title_face = "plain"))
```

### 12. Other key differences to ggplot2
Expand Down

0 comments on commit b5d30f2

Please sign in to comment.