使用trellis doubleYScale图更改线型

时间:2012-12-12 15:40:10

标签: r

我创建了一个doubleYScale剧情:

obj1 <- xyplot(Produktion ~ Year, datac1mw, type = "l",col="black")
obj2 <- xyplot(Summa.skulder ~ Year, datac1mw, type = "p",col="black")

hm=doubleYScale(obj1, obj2, add.ylab2 = TRUE,text = c("Produktion", "Summa.skulder"), 
             par.settings = simpleTheme(col = c('blue','black'), lty = 2:3))

我得到的结果是: enter image description here

我真正想要的是一个情节,其中legend不显示2条线(现在是蓝线和红线)但是Produktion(左侧y轴)是点缀的并且Summa.Skulder(右侧y- axis)是图例中的一条线。

有什么建议吗? 最诚挚的问候

2 个答案:

答案 0 :(得分:2)

这对我来说很好用:

library(lattice)
library(latticeExtra)
x <-1:10 # make data as I do not have yours
y <-1:10
z <-10:1

obj1 <- xyplot(y ~ x, type = "l",col="black")
obj2 <- xyplot(z ~ x, type = "p",col="black")

doubleYScale(obj1, obj2, add.ylab2 = TRUE)
update(doubleYScale(obj1, obj2, text = c("obj1", "obj2")),
par.settings = simpleTheme(col = c("black","black"), lty = 1:2))

enter image description here

这可用于获取图例中的点和线,如新评论所示:

obj1 <- xyplot(y ~ x, type = "l",col="black",key = simpleKey(col=c('black'),
           text=c("Produktion"),points=FALSE, lines=TRUE))
obj2 <- xyplot(z ~ x, type = "p",col="black",key = simpleKey(col=c('black'),
           text=c("Summa.skulder"),lines=FALSE,points= TRUE))

doubleYScale(obj1, obj2, add.ylab2 = TRUE,par.settings = simpleTheme(col =   
              c("black","black")))

enter image description here

答案 1 :(得分:0)

图例设置仅来自obj1。因此最好在obj1中设置参数。

您可以在obj2中设置面板设置(我添加示例作为选项)

foo <- list(x = 1:100, y = cumsum(rnorm(100)))
obj1 <- xyplot(y ~ x, foo,
               key = simpleKey(
                 text=c("Produktion", "Summa.skulder"),
                 col=c('blue','black'),points= FALSE,rectangles=TRUE,lines=TRUE))
obj2 <- xyplot(y^2 ~ x, foo
               , panel = function(...)panel.xyplot(type = "l",lty = 2,grid=TRUE,...))
## simple case: no axis for the overlaid plot
doubleYScale(obj1, obj2,add.ylab2 =TRUE)

enter image description here