R点阵条形图:指定图例的颜色? [英] R lattice bar chart: specify colour of legend?

查看:193
本文介绍了R点阵条形图:指定图例的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

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为我为每一行打印一个直方图,其中列为直方图的条形图.

I want R to print me a histogram for each row, with the columns as the bars of the histogram.

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

I've got this so far:

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)

这是有效的,但它只是更改条形的颜色,而不是图例的颜色.

It's working, but it's only changing the colour of the bars, not the colour of the legend.

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

How can I specify the colour of the legend as well as the bars?

推荐答案

如果不更改par.settings,则不愿将col=参数传递给barchart,而是更倾向于点阵.在这种情况下.条形的颜色由superpose.polygon决定,因为您有不同的等级组.这应该做你想要的

Rather than passing a col= parameter to barchart, lattice much prefers if you change the par.settings. In this case. the color of the bars is determined by superpose.polygon because you have different groups of ratings. This should do what you want

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点阵条形图:指定图例的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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