Q:

Rounded corners in ggplot2?

This feels like it has to be possible, but I cannot figure out if I can do it relatively simply in a theme() or have to dig deeper. 
I would like to round the corners on the plot areas of my ggplot2 plots. How would I go about doing that? 
Question inspired by: https://twitter.com/Hoog10HK/status/951305194809143296

A:

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.

