13

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

5
  • 1
    Someone has done the "this doesn't feel right" approach before: r-bloggers.com/rounded-corners-in-ggplot2-graphics
    – Jon Harmon
    Commented Jan 11, 2018 at 4:58
  • Mmm: a version of element_rect that takes a couple of extra options for roundedness and switches in a grid.roundedRect (or one wrapped by a gTree, maybe) seems like the intuitive way to go
    – jimjamslam
    Commented Jan 11, 2018 at 5:01
  • 1
    @rensa I'll need to dig into exactly what element_rect actually returns. If it's possible to put the roundedness in there without breaking everything that uses the return, it definitely feels possible.
    – Jon Harmon
    Commented Jan 11, 2018 at 5:11
  • 1
    @rensa: element_rect is just a data structure. You can't make it draw rounded rectangles. Commented Jan 12, 2018 at 6:38
  • There is now a package for rounded corners in ggplot2: github.com/botan/ggrounded I haven't dug in to see how it works.
    – Jon Harmon
    Commented Dec 9, 2023 at 19:26

2 Answers 2

20

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)

enter image description here

The same trick could be applied to the panel background, legend background, etc., if you wanted other aspects of the plot to be rounded.

1
  • I guess we were overengineering the problem a little; I forgot you could just go in and modify the grobs after plotting!
    – jimjamslam
    Commented Jan 12, 2018 at 6:41
10

Thanks to theme element subclassing in ggplot2 v3.0.0, I was able to implement a more general approach in a github package with elementalist::element_rect_round(). (disclaimer: I wrote that github package)

library(ggplot2)
library(elementalist) # devtools::install_github("teunbrand/elementalist")

ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
  geom_point() +
  facet_grid(~ cyl) +
  theme(
    legend.background = element_rect_round(radius = unit(0.2, "snpc")),
    legend.key = element_rect_round(radius = unit(0.4, "snpc")),
    panel.background = element_rect_round(radius = unit(1, "cm")),
    strip.background = element_rect_round(radius = unit(8, "pt")),
    plot.background  = element_rect_round(fill = "#C4E7FF")
  )

Created on 2021-07-05 by the reprex package (v1.0.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.