1

This is not a duplicate question. I understand there are answers that explain how to change the color of the panel background within a facet plot. That's not what I'm after, please understand, I want to change the color of the strip label background within a facet plot based on the value of the label within that facet, and not the color of the background of the plot that is below the facet label.

Original Question:

I need the color of the strip.background to depend on the value within the background. The closest solution I found was this: Change text color for single facets in ggplot2

However, the solution specifies which color goes where, it does not allow the color to change depending on the value.

#Using iris data

i <- iris
levels(i$Species)

#Lable function to pass to facet_grid

labels=function(variable, value){
  temp=lapply(value,function(v)
    as.character(max(i$Sepal.Length[which(i[,variable]==v)]))
  )
  unlist(temp)
}

ggplot(i, aes(Petal.Length)) + stat_bin()+
  facet_grid(Species ~ .,labeller=labels)

The labels in this case turn out to be 5.8, 7, 7.9. How can I implement something like: if the label value <6 then the color of the facet label background should be green, if 6 < value < 7.5 color should be blue, if value if > 7.5 color should be red?

To make it just a bit more complicated (my actual dataset rather large), I need this function to ignore the first and last facet.

Community
  • 1
  • 1
  • Did you look [here](http://stackoverflow.com/questions/9847559/conditionally-change-panel-background-with-facet-grid)? – David Arenburg May 13 '14 at 13:29
  • I couldn't get that solution to work. That solution makes changes to geom_rect, which is not connected to the label header within facet_grid (at least I don't think it is). I need to change the color within the facet label, not within the plot background. – user3632598 May 13 '14 at 13:42
  • I tested baptiste's answer on the duplicate, and it still works for me. That level of grid hacking is the best you're going to be able to do. – joran May 14 '14 at 04:00
  • Yes, thank you, the link solves this issue for me. – user3632598 May 14 '14 at 15:43

0 Answers0