2

I would like to change the colours of the strip backgrounds to a predefined order.

This code generates the plot, and changes the strip backgrounds to red:

p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill="red"))

I'd like to do something like the below however, which ideally would specify a different colour for each strip

p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(. ~ cyl) +
theme(strip.background = element_rect(fill=c("red","green","blue","yellow")))

Which just makes them all red...

This was asked in similar questions years ago, the answer was to manipulate grobs. I was hoping that there was a simpler solution in the years since?

Community
  • 1
  • 1
Alex
  • 971
  • 4
  • 15
  • 28
  • 3
    I think the answer is still "grobs". The reason being that facets are more concerned with comparable visualisation of data subset by factors, so their appearance should also be comparable. A better solution might be to colour the points: `geom_point(aes(color = factor(cyl))) + scale_color_manual(values = c("red", "green", "blue", "yellow"))`. – neilfws Apr 04 '17 at 06:26
  • Maybe worth submitting an issue at GitHub? – zx8754 Apr 04 '17 at 06:54
  • Not an answer, but what I do is put some highly transparent background to each facet to make it distinguishable. – Roman Luštrik Apr 04 '17 at 08:46
  • @Roman Luštrik Do you just draw on rectangles to the size of the facets? what is the call to get the facet dimensions returned? – Alex Apr 04 '17 at 22:25
  • Basically, yes. I use `Inf` for limits. – Roman Luštrik Apr 05 '17 at 07:05

0 Answers0