ggplot2: Overplotting In a Faceted Scatterplot

[This article was first published on Learning R, 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.

Hadley Wickham recently shared a nice tip on how to get a faceted scatterplot plot with all points in the background of each plot.

This technique makes a clever use of setting the faceting variable to NULL so that all points are plotted in light grey in all the facets.

> library(ggplot2)
> ggplot(mtcars, aes(cyl, mpg)) + geom_point(data = transform(mtcars,
     gear = NULL), colour = "grey80") + geom_point() +
     facet_grid(~gear) + theme_bw()
overplotting-004.png

Update 17 May 2010

bch asked in the comments below, how to achieve the same when there are two facets. The method is the same, now one would need to exclude both of the facetting variables from the dataset used to draw the light grey points.

> ggplot(mtcars, aes(cyl, mpg)) + geom_point(data = mtcars[,
     !names(mtcars) %in% c("am", "gear")],
     colour = "grey80") + geom_point() + facet_grid(am ~
     gear) + theme_bw()
overplotting-006.png

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

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)