0
p <- ggplot(data=mtcars, aes(x=mpg, y=disp))  +geom_point(data=mtcars, color='grey') + geom_point(data=mtcars, aes(color=as.factor(gear)))   +facet_wrap(~gear)

gives this: enter image description here

But what I want is all data greyed out in the background.

This gets closer but makes background data a new facet, I want it in the background of the 3 facets, not as a new one:

p <- ggplot(data=mtcars, aes(x=mpg, y=disp)) +
  geom_point(data=mtcars %>% mutate(gear = "All"), aes(group=gear), color='grey') +
  geom_point(aes(color=as.factor(gear))) +
  facet_wrap(~gear)

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
curious
  • 640
  • 4
  • 10

1 Answers1

1

You could use one geom_point with the gear column using select and color them grey. For the second geom_point you could color them by factor like this:

library(ggplot2)
library(dplyr)

p <- ggplot(data=mtcars, aes(x=mpg, y=disp))  + 
  geom_point(data = mtcars %>%
               select(-gear), aes(x = mpg, y = disp), color = 'grey') +
  geom_point(aes(color=as.factor(gear))) +
  facet_wrap(~gear) 
p

Created on 2023-03-29 with reprex v2.0.2


If you want to display "all" to the legend with grey color you could assign the name to the aesthetics with color and change the colors with scale_color_manual like this:

library(ggplot2)
library(dplyr)
p <- ggplot(data=mtcars, aes(x=mpg, y=disp))  + 
  geom_point(data = mtcars %>%
               select(-gear), aes(x = mpg, y = disp, color = 'all')) +
  geom_point(aes(color=as.factor(gear))) +
  scale_color_manual(breaks=c("3","4","5","all"),
                    values=c("red", "blue", "green", "grey")) + 
  facet_wrap(~gear) 
p

Created on 2023-03-29 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53