Skip to content

Instantly share code, notes, and snippets.

@gkaramanis
Last active February 22, 2020 15:07
Show Gist options
  • Save gkaramanis/7d22ae6adba5553092884cd9513bfd31 to your computer and use it in GitHub Desktop.
Save gkaramanis/7d22ae6adba5553092884cd9513bfd31 to your computer and use it in GitHub Desktop.
Add "background" to axis title
library(ggplot2)
library(grid)
myGrob <- grobTree(rectGrob(gp = gpar(fill = "purple", alpha = 0.5)))
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
annotation_custom(myGrob, xmin = -10, xmax = -1, ymin = -10, ymax = Inf) + # x axis
annotation_custom(myGrob, xmin = -10, xmax = Inf, ymin = -10, ymax = -1) + # y axis
coord_cartesian(xlim = c(0, 10), ylim = c(0, 10), clip = "off") +
theme(
plot.margin = margin(0, 0, 100, 100)
) +
ggsave("~/Desktop/axis-bg.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment