Here's the code I'm using to plot a regression line for each US state, using facet wrapping in ggplot2:
ggplot(data = allYearsSlope, aes(x = year, y = residual)) +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~ state, ncol = 8) +
theme(
strip.text = element_text(size = rel(0.5), face = "bold"),
panel.background = element_rect(fill = "white"),
panel.border = element_rect(color="purple",fill=NA),
plot.background = element_blank(),
axis.ticks.x = element_blank()
) +
scale_x_continuous(labels = NULL)
It works fine. But now I'm trying to color the facet panel label (the box that, in this case, contains the name of the state that appears above each panel) according to a value ("blue" or "green") that I'm storing in the dataframe that contains the rest of the data. The color is stored in allYearsSlope$panelColor. Having a really hard time figuring out how to do this.
I've read the solution here: Conditional formatting of panel background in GGplot2. Along with the few other SO answers it refers to. But I don't really see a way to do this coming out of those answers. Any ideas, anyone?
Big thanks for any help.