【发布时间】:2018-11-17 07:49:18
【问题描述】:
我正在尝试调整我的条形图,以便只有少数标签以不同的颜色显示。我知道之前有一个类似的问题 (Change the color of a subset of group labels in a boxplot in R),但答案对我没有帮助,我在网上找到的所有其他内容也没有,所以我真的非常感谢一些帮助。
我的数据如下所示:
trait2 <- c('A','B','C','D')
rg <- c (0.5480, 0.4801, 0.2805, -0.2480)
se <- c(0.0495, 0.0908, 0.0548, 0.0957)
data <- data.frame(trait2,rg,se)
data
trait2 rg se
A 0.5480 0.0495
B 0.4801 0.0908
C 0.2805 0.0548
D -0.2480 0.0957
条形图的代码如下所示:
barplot1 <- barplot(data$rg,
main="correlation between traits",
xlab="rG",
border="blue",
las=1,
horiz=TRUE,
names.arg=data$trait2,
font.axis=2
cex.names=0.5,
xlim=range(-0.4,0.6,0.1) )
我现在想更改 A 和 C 的标签颜色,但 B 和 D 应该保持黑色。
我试过这个,正如我在上面提到的链接中描述的那样:
mtext(barplot1$trait2, at = 1:length(barplot1$trait2, side=1, line=1,
col=ifelse(barplot$trait1=="A", "red", "black")))
然后我收到错误“barplot$trait2 中的错误:$ 运算符对原子向量无效。这是有道理的,但我仍然不知道如何解决它。
还有一个使用的建议
+ scale_colour_manual(values = c("B" = "red"))
我真的无法理解。 (我应该在哪里插入?)
希望有人能帮助我,在此先感谢!
【问题讨论】:
-
scale_colour_manual()是ggplot2包中的一个函数。该软件包为您提供了更灵活的图表创建方式。
标签: r plot colors label customization