ggplot2: Overplotting In a Faceted Scatterplot

December 3, 2009
By

(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()
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 his blog: Learning R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.