更改ggplot2中单个构面的文本颜色 [英] Change text color for single facets in ggplot2

查看:123
本文介绍了更改ggplot2中单个构面的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下命令创建了下图:

  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 )+
主题(strip.text.y = element_text(color ='red4'))

如果我想只为上面的x facet(在这种情况下为'Implicit'和'Explicit'levels)改变文本颜色(也可能是背景颜色),我该怎么做?可能吗?



Andrea





编辑:我很抱歉混淆。我的目标是改变其中一个上方条的文字和背景颜色,而不是颜色方面。

您想更改条带元素的属性,而不是方面。尝试像下面的代码。请注意,这是一个基于随机构成的假数据的最小示例,因为您没有提供自己的数据供我们使用。

 要求(重塑)
要求(ggplot2)$ b您必须根据您的需求调整代码。 $ b $ require(scale)

#假数据
mydf < - data.frame(val1 = runif(10,0,1),val2 = runif(10,0,1) )
mydf

#重塑为长格式
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(color ='blue',size = 10))

这会产生如下图:



请注意,您已经等了很长时间(按照Stack Ov上的R社区标准erflow)来回答问题,因为您的问题并不清楚,因为您没有提供完全可复制的代码和数据,我们可以将其复制并粘贴到我们自己的R安装中。如果你这样做了,那么比我更有知识的人会在一个小时内回答这个问题。请参阅这篇非常有用的帖子,了解如何提问的提示你的下一个问题。


I have created the plot below with these 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 text color (and possibly also the background color) for only the upper x facet (in this case 'Implicit' and 'Explicit' levels), how can I do that? Is it possible? I have not read nothing about that in the ggplot2 documentation.

Andrea

EDIT: I'm sorry for the confusion. My aim is to change the text and background color of one of the upper strips, not the color of the facet.

解决方案

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.

这篇关于更改ggplot2中单个构面的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆