user1665355
user1665355

Reputation: 3393

Changing the line types with trellis doubleYScale plot

I did create a doubleYScale plot:

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

The result I get is: enter image description here

What I really want is a plot where the legend shows not 2 lines (as now a blue and a red line) but that Produktion (left y-axis) is dotted and Summa.Skulder (right y-axis) is a line, in the legend.

Any suggestions? Best Regards

Upvotes: 2

Views: 1464

Answers (2)

user1317221_G
user1317221_G

Reputation: 15461

This works fine for me:

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

This works for getting points and lines in the legend as your new comment says:

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

Upvotes: 2

agstudy
agstudy

Reputation: 121608

Legend settings ares taken only from obj1. So it is better to set the key parmeter in the obj1.

You can set the panel settings in the obj2( I add an example as option)

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

Upvotes: 0

Related Questions