gpt4 book ai didi

r - R 条形图的组着色错误

转载 作者:行者123 更新时间:2023-12-01 10:27:34 25 4
gpt4 key购买 nike

我正在尝试使用 R 创建条形图,但图例中的组着色看起来不对。

enter image description here

data = c(29,5,22,12,20,11,14,15,21,8)

colors = c(gray.colors(1, start = .1),gray.colors(1, start = .1),
gray.colors(1, start = .3),gray.colors(1, start = .3),
gray.colors(1, start = .5),gray.colors(1, start = .5),
gray.colors(1, start = .7),gray.colors(1, start = .7),
gray.colors(1, start = .9),gray.colors(1, start = .9))

names = c('1','1','2','2','3','3','4','4','5','5')

barplot(rev(data), horiz=TRUE, col = rev(colors), names.arg = rev(names),
legend.text = rev(c("1","2","3","4","5")), las=1, xlim = c(0,30),
args.legend = list(x ='bottomright', inset=c(0,0.05))
)

我可以想象是什么原因造成的。我最初的猜测是,我应该使用矩阵而不是向量,然后设置 beside = True,但是当我这样做时,条形分布不均匀。

最佳答案

您可以覆盖图例的 fill 选项。

barplot(rev(data), horiz=TRUE, col = rev(colors), 
names.arg = rev(names),
legend.text = rev(c("1","2","3","4","5")),
las=1, xlim = c(0,30),
args.legend = list(x ='bottomright', inset=c(0,0.05),
fill=unique(colors))
)

关于r - R 条形图的组着色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com