2

我确实创建了一个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))

我得到的结果是: 在此处输入图像描述

我真正想要的是一个图,其中legend显示的不是 2 条线(现在是一条蓝线和一条红线),而是 Produktion(左 y 轴)是虚线,而 Summa.Skulder(右 y 轴)是一条线,在传奇。

有什么建议么?此致

4

2 回答 2

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))

在此处输入图像描述

正如您的新评论所说,这适用于获取图例中的点和线:

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")))

在此处输入图像描述

于 2012-12-12T16:35:45.087 回答
0

图例设置仅取自 obj1。所以最好在obj1中设置key参数。

您可以在 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)

在此处输入图像描述

于 2012-12-12T16:54:22.350 回答