且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改ggplot2中单个构面的文本颜色

更新时间:2023-11-26 23:49:04

You want to change the attributes of the strip element, not the facet. Try something like the code below. Note that this is a minimal example based on fake data made up at random, as you did not provide your own data for us to work with. You'll 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 produces a plot like this:

Please note that you have waited quite a while (by the standards of the R community on Stack Overflow) for an answer because your question wasn't clear and because you didn't provide fully reproducible code and data that we can copy and paste into our own R installations. If you had done that, somebody far more knowledgeable than myself would have answered this question within an hour. Please see this very useful post for tips on how to ask your next question.