Dieter Menne
2011-Feb-07 07:40 UTC
[R] lattice: Synchronizing key and lines; or: why is lty used as lwd in legend?
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>
Deepayan Sarkar
2011-Feb-08 09:51 UTC
[R] lattice: Synchronizing key and lines; or: why is lty used as lwd in legend?
On Mon, Feb 7, 2011 at 1:10 PM, Dieter Menne <dieter.menne at menne-biomed.de> wrote:> 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.This looks like a bug newly introduced in the recent lattice update, your examples work as expected in earlier versions. I will look into it. In the meantime, going back to version 0.19-13 may help. -Deepayan> 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 >> > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >