gguidance
gguidance copied to clipboard
A 'ggplot2' extension that focusses on expanding the plotter's arsenal of guides, such as axes, legends and colour bars. It brings various improvements to existing guides by re-implementation, as...
gguidance ![](https://github.com/teunbrand/gguidance/raw/main/man/figures/logo.png)
Warning You’re looking at an experimental branch that explores guide extensions with an upcoming ggproto overhaul in ggplot2.
The goal of gguidance is to provide additional guides to the ggplot2 ecosystem.
Please note that this repo is still being worked on and, while probably usable, isn’t finished.
Installation
You can install the development version of gguidance from GitHub with:
# install.package("pak")
pak::pak("tidyverse/ggplot2") # Installs development version of ggplot2
pak::pak("teunbrand/gguidance@main") # Installs this package
Overview
A table of contents of this package’s utilities:
-
Legends
- Cross legend
- String legend
-
Colour bars
- Capped colour bar
- Violin colour bar
- Histogram colour bar
-
Axes
- Subtitles
- Minor ticks
- Capping
- Ranges
- Table
-
Grids
- Regular
- Plus
- Zebra
Examples
Let’s first set up a basic plot to experiment with
library(gguidance)
#> Loading required package: ggplot2
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
labs(
x = "Engine displacement",
y = "Highway miles per gallon"
) +
theme(axis.line = element_line())
Legends
Cross legend
A ‘cross legend’ can show two variables in a single legend.
p + aes(colour = paste(cyl, year)) +
guides(colour = "legend_cross")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-legend_cross-1.png)
String legend
A string legend doesn’t display keys, but colours the labels.
p + aes(colour = class) +
guides(colour = "legend_string")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-legend_string-1.png)
Colour bars
Capped colour bar
A capped colour bar:
p + aes(colour = cty) +
scale_colour_viridis_c(guide = "colourbar_cap")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-colourbar_cap-1.png)
Violin colour bar
Using a violin as a colour guide:
p + aes(colour = cty) +
scale_colour_viridis_c(guide = guide_colour_violin(density = mpg$cty))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-colour_violin-1.png)
Histogram colour bar
Using a histogram as a colour guide:
p + aes(colour = cty) +
scale_colour_viridis_c(
guide = guide_colour_histogram(hist = hist(mpg$cty, breaks = 10))
)
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-colour_histogram-1.png)
Axes
Subtitles
Using subtitles.
p + guides(x = guide_axis_extend(subtitle = c("Less", "More")))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-axis_extend-1.png)
Minor ticks
Using minor ticks.
p + guides(x = "axis_minor")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-axis_minor-1.png)
Capping
Using capped lines.
p + guides(x = "axis_cap")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-axis_cap-1.png)
Ranges
With bracketed ranges.
boxplot <- ggplot(mpg, aes(class, displ)) +
geom_boxplot() +
labs(
x = "Type of car",
y = "Engine displacement"
)
boxplot +
guides(x = guide_axis_nested(
range_start = c(0.5, 3.5),
range_end = c(4.5, 6.5),
range_name = c("First range", "Second range"),
bracket = "square"
))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-axis_nested-1.png)
Table
Using a table as an axis guide.
# Creating summary table
my_table <- lapply(split(mpg[, c("displ", "cty", "hwy")], mpg$class), colMeans)
my_table <- as.data.frame(do.call(rbind, my_table))
my_table[] <- lapply(my_table, scales::number, accuracy = 0.01)
my_table$class <- rownames(my_table)
# Use summary table as axis guide
boxplot +
guides(x = guide_axis_table(table = my_table, key_col = class))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-axis_table-1.png)
Grids
Regular
Like the vanilla panel grid, but with more options for where breaks appear. Here, between categories.
boxplot +
coord_guided(guide_grid(x_breaks = breaks_between()))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-regular_grid-1.png)
Plus
Subtler grid lines by only drawing the intersections, making a ‘plus’ symbol.
p + coord_guided("grid_plus")
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-plus_grid-1.png)
Zebra
Alternating stripes between breaks.
boxplot + coord_guided("grid_zebra") +
theme(panel.grid.major.x = element_line(alpha("grey50", 0.1)))
![](https://github.com/teunbrand/gguidance/raw/main/man/figures/README-zebra_grid-1.png)