如何避免在条形图中回收颜色以在每组中实现不同的颜色?

时间:2015-08-05 18:41:59

标签: r bar-chart

这与我刚问过的another问题密切相关。

这是我的数据:

y <- structure(c(0.5619, 0.4381, 0.7587, 0.2413, 0.8764, 0.1236, 0.9019, 
               0.0981, 0.9481, 0.0519, 0.99, 0.01),
            .Dim = c(2L, 6L), .Dimnames = list(c("FALSE", "TRUE"), NULL))
y
#         [,1]   [,2]   [,3]   [,4]   [,5] [,6]
# FALSE 0.5619 0.7587 0.8764 0.9019 0.9481 0.99
# TRUE  0.4381 0.2413 0.1236 0.0981 0.0519 0.01

每组中的相同颜色的原始图(蓝色和红色):

barplot(y, horiz = TRUE, col = c("blue", "red"),
        names.arg = c("Overall", paste("Flag", 5:1)), las = 1,
        cex.names = 0.6,
        main = "Proportion Dropped Given Each Sample Restriction")

bars

我想更改每个组的红色右侧栏,而是在每个组中包含不同的颜色,例如:

ideal_output

因此,我创建了一个新的col向量,每个条形段都有一种颜色:

barplot(y, horiz =TRUE,
        col = c("blue", "gold",
                "blue", "springgreen",
                "blue", "orange",
                "blue", "red",
                "blue", "white",
    names.arg = c("Overall", paste("Flag", 5:1)), las = 1,
    cex.names = 0.6,
    main = "Proportion Dropped Given Each Sample Restriction"))

但是,只使用col(蓝色和金色)中的前两种颜色,它们被循环使用6次:

enter image description here

有没有办法获得我正在寻找的输出?

1 个答案:

答案 0 :(得分:2)

也许通过欺骗barplot来思考还有更多类别。这非常难看,但它似乎完成了工作:

ymod <- cbind(c(y[,1], 0, 0,0,0,0,0,0,0,0,0),
              c(0, 0, y[,2],0,0,0,0,0,0,0,0),
              c(0, 0,0,0, y[,3],0,0,0,0,0,0),
              c(0, 0,0,0,0,0, y[,4],0,0,0,0),
              c(0, 0,0,0, 0,0,0,0,y[,5],0,0),
              c(0, 0,0,0, 0,0,0,0,0,0,y[,6]))

barplot(ymod, horiz=T,
        col=c(rbind(rep("blue",6),c("white","red","purple",
                                    "orange","springgreen","gold"))))

enter image description here

Herehere是我用来实现此结果的一些参考。