如何避免在 barplot 中回收颜色以在每个组内实现不同的颜色?
How to avoid recycling of colors in barplot to achieve different colors within each group?
这与我刚刚提出的 问题密切相关。
这是我的数据:
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 = 1L,
cex.names = 0.6,
main = "Proportion Dropped Given Each Sample Restriction"
)
我想更改每个组右侧的红色条,改为在每个组中使用 不同 颜色,例如:
因此,我创建了一个新的 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 = 1L,
cex.names = 0.6,
main = "Proportion Dropped Given Each Sample Restriction"
)
然而,只使用了col
中的前两种颜色(蓝色和金色),它们被回收了6次:
有什么方法可以获得我正在寻找的输出吗?
也许通过欺骗 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"))))
Here and here 是我用来实现此结果的一些参考。
这与我刚刚提出的
这是我的数据:
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 = 1L,
cex.names = 0.6,
main = "Proportion Dropped Given Each Sample Restriction"
)
我想更改每个组右侧的红色条,改为在每个组中使用 不同 颜色,例如:
因此,我创建了一个新的 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 = 1L,
cex.names = 0.6,
main = "Proportion Dropped Given Each Sample Restriction"
)
然而,只使用了col
中的前两种颜色(蓝色和金色),它们被回收了6次:
有什么方法可以获得我正在寻找的输出吗?
也许通过欺骗 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"))))
Here and here 是我用来实现此结果的一些参考。