Change text color for individual faces in ggplot2

I created the chart below using the following commands:

ggplot(long.data, aes(owner,value)) + stat_summary(fun.y=mean,geom="bar", fill=c("deepskyblue","deepskyblue4")) + stat_summary(fun.data=mean_cl_normal,geom="errorbar",position= position_dodge(width=.90),width=.1) + labs(x="",y="") + facet_grid(IV~experiment+type,scales="free_y") + theme(strip.text.y = element_text(colour = 'red4')) 

If I want to change the color of the text (and possibly also the background color) only for the top face x (in this case, β€œImplicit” and β€œExplicit” levels), how can I do this? Is it possible? I have not read anything about this in the ggplot2 documentation.

Andrea

plot

EDITOR: I'm sorry for the confusion. My goal is to change the color of the text and background of one of the upper stripes, not the color of the facet.

+4
source share
2 answers

You want to change the attributes of the strip element, not the facet. Try something like the code below. Please note that this is a minimal example based on fake data made at random, since you did not provide your own data for work. You will have to adapt the code to your needs.

 require(reshape) require(ggplot2) require(scales) # fake data mydf <- data.frame(val1 = runif(10, 0, 1), val2 = runif(10, 0, 1)) mydf # reshape to long format long.data <- melt(mydf) long.data$facetvar <- "implicit" long.data$facetvar[seq(1, 19, 2)] <- "explicit" long.data # plot ggplot(long.data, aes(y = value, x = variable)) + geom_bar(position = 'dodge', stat = "identity") + facet_wrap (~ facetvar) + theme(strip.background = element_rect(fill = alpha('green', 0.3))) + theme(strip.text.x = element_text(colour = 'blue', size = 10)) 

This creates a graph like this: screenshot

Please note that you have been waiting a long time (according to the R on Stack Overflow community standard) for an answer because your question was unclear and because you did not provide fully reproducible code and data that we can copy and paste into our own R installations. If if you did, someone much more knowledgeable than me could answer this question within an hour. Please see this very useful post to find out how to ask your next question.

+6
source

I get a warning ... but this seems like a good starting point for a more elegant solution:

 ggplot(mtcars) + geom_rect(data = subset(mtcars, cyl == 4), aes(fill = cyl),xmin = -inf,xmax = Inf, ymin = -Inf,ymax = Inf, alpha = 0.05) + geom_point(aes(mpg, wt)) + facet_grid(. ~ cyl) 

enter image description here

+2
source

All Articles