gpt4 book ai didi

R格子条形图: specify colour of legend?

转载 作者:行者123 更新时间:2023-12-01 09:57:15 25 4
gpt4 key购买 nike

我的数据是这样的:

service,rating_1,rating_2,rating_3,rating_4,rating_5
renew_patent,0,0,1,2,11
apply_benefit,21,20,121,828,1744
apply_employment_tribunal,0,0,0,0,0

我希望 R 为每一行打印一个直方图,列作为直方图的条形。

到目前为止我已经知道了:

require(lattice)
data <- read.csv("test.csv", header = TRUE)
colors = c('red', 'orange', 'yellow', 'blue', 'green')
barchart(rating_1+rating_2+rating_3+rating_4+rating_5 ~ service, data=data,
auto.key=list(space='right'), scales=list(x=list(rot=45)),
ylab="Percentage of total", col=colors)

它起作用了,但它只是改变了条形图的颜色,而不是图例的颜色。

如何指定图例和条形的颜色?

最佳答案

与其将 col= 参数传递给 barchart,lattice 更喜欢更改 par.settings。在这种情况下。条形的颜色由 superpose.polygon 决定,因为您有不同的评分组。这应该做你想做的

data<-data.frame(
service = c("renew_patent", "apply_benefit", "apply_employment_tribunal"),
rating_1 = c(0, 21, 0),
rating_2 = c(0, 20, 0),
rating_3 = c(1, 121, 0),
rating_4 = c(2, 828, 0),
rating_5 = c(11, 1744, 0)
)

colors = c('red', 'orange', 'yellow', 'blue', 'green')
barchart(rating_1+rating_2+rating_3+rating_4+rating_5 ~ service, data=data,
auto.key=list(space='right'), scales=list(x=list(rot=45)),
ylab="Percentage of total", par.settings=list(superpose.polygon=list(col=colors)))

关于R格子条形图: specify colour of legend?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23528122/

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