rdataframeggplot2scatter-plotfacet-wrap

Show all other groups in grey behind each panel


I have this dataframe (see the code below) and the following plot.

What I need is a plot like the mock-up attached (made in PowerPoint): In each facet panel, I want to show in light grey all the data points from all groups, and on top of them the coloured points of the group corresponding to that facet.

So: grey background points = whole dataset; coloured points = just the facet’s group; facet per group.

set.seed(123)   # for reproducibility

df_example <- data.frame(
  a = rnorm(10, mean = 5, sd = 2),         # numeric
  b = runif(10, min = 0, max = 10),        # numeric
  c = factor(sample(c("L1","L2"), 10, replace = TRUE))
)

ggplot( ) +  
  geom_point(data = df_example,
             aes(x = a, y = b, group=c, fill=c ),
             size=4, stroke = 1,
             # fill="orange" ,
             colour="black",pch=21  ) +
  scale_fill_manual(values=c('pink','darkorchid1'))+facet_wrap(~ c) 

what I get now: current plot

What I need: what I'd like to get


Solution

  • I've found a solution thanks to @markus here: Add other series to panel/facet plot using ggplot2

    Here the plot from the example I gave:

    ggplot( data = df_example,aes(x = a, y = b)) +  
      geom_point(aes(group=c),#point to have gray dots in every panel
                 size=4, stroke = 1,
                 colour="gray30",fill='gray80',pch=21  ) +
      geom_point(data = transform(df_example, c2 = c),#points for the faceting
                 aes( group=c, fill=c ),
                 size=4, stroke = 1,
                 colour="black",pch=21  )+
      scale_fill_manual(values=c('pink','darkorchid1'))+facet_wrap(~ c2) 
    

    And here attached the plot I got from my own data:enter image description here