简体   繁体   English

R:更改堆叠条形图中各个条的颜色

[英]R: change colors across bars in stacked bar chart

Using base graphics, I got a barchart like this one 使用基本图形,我得到了一个像这样的条形图

a <- c(1,2,3); b <- c(1,1,1)
barplot(rbind(a,b), col=c("red","gray"))

在此处输入图片说明

with each bar grey at the top and red at the bottom. 每个栏的顶部为灰色,底部为红色。 However, I would like each bar to change the color at the bottom and keep the grey at the top. 但是,我希望每个栏在底部更改颜色,在顶部保留灰色。

cols <- c(rbind(c("red","green","blue"),"gray"))
barplot(rbind(a,b), col=cols) 

did not do the trick. 没有做到这一点。 Is there any other way? 还有其他办法吗?

Not the most elegant way but this works. 这不是最优雅的方法,但这可行。

a<-c(1,1,1)
b<-c(1,0,0)
c<-c(0,2,0)
d<-c(0,0,3)

cols <-c("red","green","blue","gray")
# notice order of rbind... a is last
barplot(rbind(b,c,d,a), col=cols )

在此处输入图片说明

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

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