2021-01 Accessing ‘grid’ from ‘ggplot2’

[This article was first published on R – Stat Tech, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This report describes the ‘gggrid’ package, which provides a convenient interface for making use of raw ‘grid’ functions in combination with ‘ggplot2’.

The ‘gggrid’ package provides two functions, grid_panel() and grid_group(), both of which create a new layer in a ‘ggplot2’ plot. The first argument to both functions is a ‘grid’ grob or a function that generates a grob and that grob is added to the plot region of the ‘ggplot2’ plot.

For example, the following code adds a rectangle filled with a semitransparent radial gradient to a ‘ggplot2’ plot.

library(gggrid)
grad <- radialGradient(c("transparent", "black"), r2=.6)
ggplot(mtcars) +
    geom_point(aes(disp, mpg)) +
    grid_panel(rectGrob(gp=gpar(fill=grad)))

Paul Murrell

Download

To leave a comment for the author, please follow the link and comment on their blog: R – Stat Tech.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)