在格子中更改图例中的顺序

时间:2016-07-09 02:20:40

标签: r lattice

我想知道是否有更改格子中图例系列的顺序?
例如,如果我有一个重叠密度a,b和c的图,但在图例中我希望订单为c,b,a,无论如何我可以做到吗?

dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5), rnorm(100,-10,5))
              , lines = rep(c("a", "b","c"), each = 100))

densityplot(~dens,data=dat,groups = lines,
        plot.points = FALSE, ref = TRUE, 
        auto.key = list(corner=c(1,0.95)))

密度图图:

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以尝试类似下面的内容,按照相反的顺序设置lines的级别:

library(lattice)

lev <- c('c','b','a')
dat <- data.frame(dens  = c(rnorm(100), rnorm(100, 10, 5), rnorm(100,-10,5)),
                  lines = factor(rep(c("a", "b","c"), each = 100),
                                levels=lev, labels=lev, ordered = TRUE))

densityplot(~dens,data=dat,groups = lines,
        plot.points = FALSE, ref = TRUE, 
        auto.key = list(corner=c(1,0.95)))

结果如下。

enter image description here