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.