键入带有两个y标签的xyplot

时间:2013-11-23 05:48:06

标签: r lattice

我想用两个轴做xyplotdoubleYScale函数可以解决问题,但我无法获得正确的密钥。

require(latticeExtra)
set.seed(1)
df <- data.frame(x = 1:100, y = cumsum(rnorm(100)), z = rep(1:2, 50), 
                 y2 = seq(30, 20, length.out = 100))
obj1 = xyplot(y~x, groups = z, data = df, type = c("l"), par.settings = 
                simpleTheme(col = 1, lty = 1:2), lty = 1:2)
obj2 = xyplot(y2~x, data = df, type = "l", par.settings = 
                simpleTheme(col = 1, lty = 3))
doubleYScale(obj1, obj2, auto.key = list(text = c("var1", "var2", "var3"), 
                points = F, lines = T, lty = 1:3, column = 3), add.ylab2 = TRUE)

生成以下图表:enter image description here

所以在键'var3'中应该用虚线画出来。

第二个问题是,尽管add.ylab2 = TRUE,右轴没有ylab。最后,为什么会有警告?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

右边的ylab需要来自其中一个对象。在这种情况下,它似乎需要来自第一个。

require(latticeExtra)
set.seed(1)
df <- data.frame(x = 1:100, y = cumsum(rnorm(100)), z = rep(1:2, 50), 
                 y2 = seq(30, 20, length.out = 100))
obj1 = xyplot(y~x, groups = z, data = df, type = c("l"), 
              ylab.right="my_ylab2", par.settings = 
                simpleTheme(col = 1, lty = 1:2), lty = 1:2)
obj2 = xyplot(y2~x, data = df, type = "l",  par.settings = 
                simpleTheme(col = 1, lty = 3))
doubleYScale(obj1, obj2, text = c("var1", "var2", "var3"), 
                par.settings = simpleTheme(col = 1, lty = 3),

add.ylab2 = TRUE)