Skip to content

Instantly share code, notes, and snippets.

@grantmcdermott
Created February 20, 2023 06:06
Show Gist options
  • Save grantmcdermott/c173a45b1ce4149605afea4ae0091ce6 to your computer and use it in GitHub Desktop.
Save grantmcdermott/c173a45b1ce4149605afea4ae0091ce6 to your computer and use it in GitHub Desktop.
Floating axes in ggplot2 (via ggh4x)
# Context: https://mastodon.social/@gmcd/109895547902826763
library(ggh4x)
theme_set(
theme_bw() +
theme(
axis.line = element_line(linewidth = 0.4),
axis.ticks = element_line(linewidth = 0.4),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(linetype = 3, colour = "lightgray"),
strip.background = element_rect(colour = NA, fill = "gray90"),
panel.border = element_blank(),
panel.spacing = unit(1, "line")
)
)
p = ggplot(mpg, aes(displ, cty, col = as.factor(cyl))) +
geom_point() +
scale_colour_viridis_d("Cylinders", option = "mako", begin = 0.2, end = .8) +
labs(x = "Engine displacement (litres)", y = "MPG (city)") +
facet_wrap(~year)
# Regular axes
p
# Floating axes
p +
guides(x = "axis_truncated", y = "axis_truncated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment