在条形图中按行属性颜色

时间:2014-09-01 13:18:42

标签: r colors stackedbarseries

我在将条形图中的颜色归因于行时遇到问题,它们不存在。当我使用命令时:

barplot(data[,3:429])

它确实给了我正确的高度模式,但它全是黑色的。我有22个不同的行(每个有427个变量),但那些在我的情节中没有识别。

一小部分数据如下所示:

VT  ATC V4  V5  V6  V7  V8  V9  V10 V11 V12 .... V429
1   1   0   1   1   2   2   2   2   1   1
1   2   0   1   1   1   1   1   1   1   1
1   3   0   0   0   0   0   0   0   0   0
1   4   0   0   0   0   0   0   0   0   0
1   5   0   0   0   1   1   1   1   0   0

我尝试过添加颜色,但它不会像这样工作:

barplot(data[,3:429],col=c("blue", "aliceblue","antiquewhite", "antiquewhite1","antiquewhite2","antiquewhite3", "antiquewhite4","aquamarine","aquamarine1","aquamarine2", "aquamarine3", "aquamarine4", "darkgreen", "darkgrey","darkkhaki","darkmagenta", "darkolivegreen","darkolivegreen1","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkorange"))

22种颜色是随机选择的,对它们没有特别的意义。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

以下为我效劳:

#dummy data
m <- matrix(sample(1:10,500*22,replace=TRUE),ncol=500)

#stacked bar plot with colours
barplot(m[,3:400],
        col=c("blue", "aliceblue","antiquewhite", "antiquewhite1","antiquewhite2","antiquewhite3", "antiquewhite4","aquamarine","aquamarine1","aquamarine2", "aquamarine3", "aquamarine4", "darkgreen", "darkgrey","darkkhaki","darkmagenta", "darkolivegreen","darkolivegreen1","darkolivegreen2","darkolivegreen3","darkolivegreen4","darkorange"),
        border = FALSE, space=0
        )

这是你期望得到的吗?

enter image description here