简体   繁体   English

更改小节R中标签子集的颜色

[英]Change the colour of a subset of labels in barplot R

I am trying to adjust my barplot so that only a few of the labels appear in a different colour. 我试图调整条形图,以便只有少数几个标签以不同的颜色出现。 I know that there was a similar question before ( Change the color of a subset of group labels in a boxplot in R ) but the answer did not help me and everything else I found online neither, so I would really, really appreciate some help. 我知道以前也有类似的问题( 在R中的箱图中更改组标签的子集的颜色 ),但是答案并没有帮助我,也没有在网上找到其他所有内容,因此,我将非常感谢您的帮助。

My data looks like this: 我的数据如下所示:

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

and the code for the barplot looks like this: 该barplot的代码如下所示:

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) )

在此处输入图片说明

I would now like to change the colour of the lables of A and C, but B and D should stay in black. 我现在想更改A和C的标签的颜色,但B和D应该保持黑色。

I tried this, as it was described in the link I mentioned above: 我尝试了此操作,正如上面提到的链接中所述:

mtext(barplot1$trait2, at = 1:length(barplot1$trait2, side=1, line=1,  
col=ifelse(barplot$trait1=="A", "red", "black")))

but then I get the error "Error in barplot$trait2 : $ operator is invalid for atomic vectors. Which makes sense, but I still don't know how to solve it. 但随后出现错误“ barplot $ trait2中的错误:$运算符对于原子向量无效。这是有道理的,但我仍然不知道如何解决。

There was another suggestion of using 还有使用的建议

 + scale_colour_manual(values = c("B" = "red"))

which I cannot really make sense of. 我实在无法理解。 (Where should I insert that?) (我应该在哪里插入?)

I hope someone can help me, thanks in advance! 希望有人能帮助我,在此先感谢!

you can try 你可以试试

barplot1 <- barplot(data$rg,
                    main="correlation between traits",
                    xlab="rG",  
                    border="blue", 
                    las=1, 
                    horiz=TRUE, 
                    font.axis=2,
                    cex.names=0.5,
                    xlim=range(-0.4,0.6,0.1))

axis(2, at = barplot1[c(1,3)], labels = data$trait2[c(1,3)], col.axis = 2)
axis(2, at = barplot1[c(2,4)], labels = data$trait2[c(2,4)], col.axis = 1)

在此处输入图片说明

in ggplot you can try 在ggplot中,您可以尝试

 ggplot(data, aes(trait2, rg)) + 
   geom_col(colour="blue", fill="grey") +
   coord_flip() +
   theme_bw() +
   theme(axis.text.y = element_text(colour = rep(c(2,1),2), size=20))

在此处输入图片说明

Use ggplot much easier and more elegant than the built in plotting library. 与内置的绘图库相比,使用ggplot更加轻松和优雅。 The scale_color_manual and scale_fill_manual can be inserted within the ggplot code. 可以在ggplot代码中插入scale_color_manualscale_fill_manual Read up on it here ( http://r4ds.had.co.nz/data-visualisation.html ). 在此处阅读有关内容( http://r4ds.had.co.nz/data-visualisation.html )。

Here's an example to color the bars. 这是为条形着色的示例。

library(tidyverse) #this package contains many other useful packages included ggplot    

data <- tibble(total = c(5, 6, 4, 3), month = c("april", "may", "june", "july")  
# tibble is used to create a type of data.frame object.

ggplot(data = data, aes(x = month, y = total, fill = type)) +
  geom_col()  

Its the "fill" arguement that changes the color. 它的“填充”论点改变了颜色。 The "color" argument is used for graph objects like lines and points. “ color”参数用于图形对象,例如线和点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM