[R] lattice: Synchronizing key and lines; or: why is lty used as lwd in legend?

Dieter Menne dieter.menne at menne-biomed.de
Mon Feb 7 08:40:12 CET 2011


Dear latticists,

Getting legend and graph line types synchronized has been a challenge in
lattice; the last example below that works looks ridiculously ugly for such
a simple job, I am sure there is a more elegant solution.

When trying this out, I found a feature which I found confusing: why are
line types (lty) "translated" to line widths (lwd) in the legend? See
attached pdf in case this should not be reproducible on your computer. 

Dieter
 

Library(lattice)
os = OrchardSprays[OrchardSprays$rowpos <4,]
os$rowpos = as.factor(os$rowpos)

# Set it globally
# This is confusing: why is lty used as lwd in legend??
trellis.par.set(superpose.line = list(col=c("blue","red","green"), 
  lwd = 2, lty=c(1,4,6)))
xyplot(decrease ~ treatment, os, groups = rowpos,
       type = "a",  auto.key =
         list(space = "right", points = FALSE, lines = TRUE))
dev.off()  

# To me, this seems to be the most consistent method if it worked.
s= simpleTheme(lty=c(1,4,6),col=c("red","blue","green"),lwd=3)
xyplot(decrease ~ treatment, os, groups = rowpos,
       par.settings=s,
       type = "a",  auto.key =
         list(space = "right", points = FALSE, lines = TRUE))

# !!! There must be an easier way to synchronize legend and plot
xyplot(decrease~treatment,data=os,groups=rowpos,
  type = "a",par.settings=s, 
  key = list(text=list(levels(os$rowpos)),
              lines=list(lty=s$superpose.line$lty,
              lwd=s$superpose.line$lwd,
              col= s$superpose.line$col)))

--------------------------------------------------------
R version 2.12.1 (2010-12-16)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] lattice_0.19-17

loaded via a namespace (and not attached):
[1] grid_2.12.1  tools_2.12.1
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lwdAsLty.pdf
Type: application/pdf
Size: 5118 bytes
Desc: lwdAsLty.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110207/29743c31/attachment.pdf>


More information about the R-help mailing list