如何避免重复绘制颜色来实现每个组中不同的颜色? [英] How to avoid recycling of colors in barplot to achieve different colors within each group?

查看:181
本文介绍了如何避免重复绘制颜色来实现每个组中不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与另一个我刚刚问的问题.

这是我的数据

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

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

The original plot with same colors within each group (blue and red):

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

我想更改每个组的右侧红色条形,而是在每个组中使用不同颜色,例如:

I want to change the red right-hand bars for each group, and instead have different colors within each group, something like:

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

I therefore created a new col vector with one color for each bar segment:

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次:

However, only the first two colors in col (blue and gold) are used and they are recycled 6 times:

有什么方法可以获取我想要的输出?

Is there any way to get the output I'm looking for?

推荐答案

也许通过欺骗barplot认为还有更多类别.这非常丑陋,但似乎可以完成工作:

Perhaps by tricking barplot into thinking there are more categories. This is tremendously ugly, but it seems to get the job done:

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

此处

Here and here are some references that I used to achieve this result.

这篇关于如何避免重复绘制颜色来实现每个组中不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆