Perhaps this is not a bug but rather my misunderstanding of lty and col. I would like to generate a legend for plots as follows: par(mfcol = c(2, 1), mar = c(5.1, 6.1, 4.1, 2.1)) matplot(t(matrix(1:8,8,20)), type="l") par(mfg = c(2, 1, 2, 1)) box(col = 0) # Rbug workaround. Thanks Martin legend((par()$usr)[1:2], (par()$usr)[3:4], as.character(1:8), lty=1:8, col=1:8, bty="y") The lines for 7 and 8 do not appear in the legend. If I do matplot(t(matrix(1:8,8,20)), type="l", lty=1:8, col=1:8) they do not appear in the graph. These appear to correspond to recycling of col for lty. I've tried a few variations on this which all mismatch the lines or colours. Is this a bug or how do I get the legend to match the plot? Paul Gilbert -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
[CC: to "R-help", since this should be of wider interest ]>>>>> "PaulG" == Paul Gilbert <pgilbert@bank-banque-canada.ca> writes:PaulG> Perhaps this is not a bug but rather my misunderstanding of lty PaulG> and col. I would like to generate a legend for plots as follows: No, there's definitely no bug here. PaulG> par(mfcol = c(2, 1), mar = c(5.1, 6.1, 4.1, 2.1)) PaulG> matplot(t(matrix(1:8,8,20)), type="l") PaulG> par(mfg = c(2, 1, 2, 1)) PaulG> box(col = 0) # Rbug workaround. Thanks Martin PaulG> legend((par()$usr)[1:2], (par()$usr)[3:4], as.character(1:8), PaulG> lty=1:8, col=1:8, bty="y") PaulG> The lines for 7 and 8 do not appear in the legend. No! They do, but you don't see them easily:> palette()[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" [8] "white" I.e., col=7 is "yellow" (per default, you can always change this!) col=8 is "white" both of which are hard to see with a white background. Try to say first par(bg = "gray80") # or something else between white and black and you will see the "yellow" and "white". PaulG> matplot(t(matrix(1:8,8,20)), type="l", lty=1:8, col=1:8) PaulG> they do not appear in the graph. as above, they do, look at it after par(bg = "gray")# or "gray80" For further ``color manipulation'' in R, please look at the help page for palette and its examples and the further pages in 'see also'. As another illustrative example for matplot(), try e.g., par(bg="light blue") palette(heat.colors(20)) matplot(t(matrix(1:20,20, 2)), type="l", lty=1:4, col=1:20, lwd=2) ------ The other ``phenomenon'' you observe really comes from matplot()'s default of "col = 1:6" (and then recycling). It makes sense to only use ``a few'' different colors in a general matplot (where lines cross,...). S default even recycles only four colors (col=1:4). Martin Maechler <maechler@stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum SOL G1; Sonneggstr.33 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1086 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin I realized after I sent the last message that one of my colours was white, but I didn't realize that matplot was cycling 1:6 and so I didn't understand why white on the legend was not white on the plot. There do seem to be two more general problems: 1/ There should be a reliable way to make sure the legend and the plots correspond. (I almost got messed up really badly in the interpretation of my results and the current situation seems prone to error.) I think what is needed is a wrapping together of all the elements describing plot elements (e.g.. type, bg, lty, col, lwd) in a way so that the same description can be passed to all related routines such as plot, matplot, legend. Perhaps this is already available as I know there have been discussions about plotting which I have not followed very carefully? 2/ It would be nice if this could be done in a way that worked in both R and Splus so that my plot methods work in both. (I don't necessarily expect the same line types and colours, but I don't want to maintain two sets of code.) Paul -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._