ggplot2: Overplotting In a Faceted Scatterplot
December 3, 2009
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() |

3 Comments
leave one →
Oh, that’s nice.
This is great, thank you! Do you happen to know if there is a way to do this when there is two facets? e.g., facet_grid(x~y)?
Thanks!
Yes it is possible – I have updated the post.