Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot - adding all data points to all facets

until now I can't find an appropriate answer, here is my short question about ggplot2 in R:

data(mtcars)
ggplot(data=mtcars, aes(x=mpg, y=wt, fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue"))+
geom_point(size=2, pch=21)+
facet_grid(.~cyl)

enter image description here

This is all fine, now I want all data points (regardless what number cyl has) in every facet (e.g. with a smooth grey below the points)?

Thanks, Michael

like image 806
mod_che Avatar asked Apr 19 '16 09:04

mod_che


1 Answers

Here is a slight simplification that makes life a bit easier. The only thing you need to do is to remove the faceting variable from the data provided to the background geom_point() layer.

library(tidyverse)
ggplot(data=mtcars, aes(x=mpg, y=wt)) + 
  geom_point(data=select(mtcars,-cyl), colour="grey") +
  geom_point(size=2, pch=21, aes(fill=factor(cyl))) + 
  scale_fill_manual(values=c("red","orange","blue")) + 
  facet_wrap(~cyl) 

like image 146
atiretoo Avatar answered Oct 13 '22 23:10

atiretoo