ggplot2: Overplotting In a Faceted Scatterplot

December 3, 2009

(This article was first published on Learning R, and kindly contributed to R-bloggers)

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()

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()

To leave a comment for the author, please follow the link and comment on their blog: Learning R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


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)