TIL: Make a plot with "broken"/"discontinuous" axes
Publish date: Mar 3, 2022
Last updated: Mar 3, 2022
Last updated: Mar 3, 2022
There are times when you may wish to add a “break” on the plot axis to help display the graphical content without sacrificing content information. Typically, a “break” (when present) is used on the y-axis. The times I want to make a plot like this, I always have a difficult time finding the desired package and approach to make plots like these. This is likely because “breaks” in ggplot parlance can mean something different from what I’m intending.
The following packages 📦 can be used to make these kinds of plots:
- R
ggbreak
: vignette, paper, GitHubgg.gap
: GitHub- To add those slashes to mark the breaks, you can use the
plotrix
package. Nice demo in this YouTube video.
- To add those slashes to mark the breaks, you can use the
- Python
brokenaxes
: GitHub
Generate some data:
# Data generation via `ggbreak` docs
d <- data.frame(x = 1:20,
y = c(rnorm(5) + 4, rnorm(5) + 20, rnorm(5) + 5, rnorm(5) + 22)
)
Below, I show how to generate a “break” in the axes using ggbreak
:
d %>%
ggplot(aes(x, y)) +
geom_col() +
ggbreak::scale_y_break(c(8, 16)) +
theme_minimal()
A slightly different approach: ggforce
d %>%
ggplot(aes(x, y)) +
geom_col() +
ggforce::facet_zoom(ylim = c(16,24))