【问题标题】:R lattice bar chart: specify colour of legend?R格条形图:指定图例的颜色?
【发布时间】:2014-05-07 20:48:00
【问题描述】:

我的数据如下所示:

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)

它正在工作,但它只是改变条形的颜色,而不是图例的颜色。

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

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    与其将col= 参数传递给barchart,不如将​​par.settings 更改为lattice。在这种情况下。条的颜色由 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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 2013-02-11
      • 1970-01-01
      • 2014-10-30
      • 2015-09-06
      • 1970-01-01
      • 2020-04-11
      • 2020-10-05
      相关资源
      最近更新 更多