You can replace the background grob with a round rectangle:
library(ggplot2)
library(grid)
# make a plot with blue background
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
theme(plot.background = element_rect(fill = "#C4E7FF"),
panel.background = element_blank(),
plot.margin = margin(20, 20, 20, 20))
# switch out background grob
g <- ggplotGrob(p)
bg <- g$grobs[[1]]
round_bg <- roundrectGrob(x=bg$x, y=bg$y, width=bg$width, height=bg$height,
r=unit(0.1, "snpc"),
just=bg$just, name=bg$name, gp=bg$gp, vp=bg$vp)
g$grobs[[1]] <- round_bg
# draw both plots side by side
cowplot::plot_grid(p, g, labels = c("rectangular", "rounded"),
scale = 0.85, hjust = 0.5, label_x = 0.5)
The same trick could be applied to the panel background, legend background, etc., if you wanted other aspects of the plot to be rounded.
element_rect
that takes a couple of extra options for roundedness and switches in agrid.roundedRect
(or one wrapped by agTree
, maybe) seems like the intuitive way to goelement_rect
is just a data structure. You can't make it draw rounded rectangles.