如何避免重复绘制颜色来实现每个组中不同的颜色?
这与另一个我刚刚问的问题.
这是我的数据
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.