I am trying to put a legend below some plots as follows: x11() par(mfcol = c(2, 1), mar = c(5.1, 6.1, 4.1, 2.1)) matplot(matrix(1:30,15,2), type="l", lty=1:2) par(mfg = c(2, 1, 2, 1)) legend((par()$usr)[1:2], (par()$usr)[3:4], c("a","b"), lty = 1:2, col=1:2, bty = "y") The letters "a" and "b" print but not the lines. If I omit par(mfg = c(2, 1, 2, 1)) then the legend prints properly but on top of the plot in the first area. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
1998-Dec-15 17:15 UTC
legend --> buglet in plot setup + legend()'s S/R incompatibility
Thank you Paul; One could argue that it is your fault, calling legend() without a preceding plot in that figure. Help says ``add legends to plots'' However, I agree this *is* a buglet somewhere. Look at this [which is your code ++ ] x11() par(mfcol = c(2, 1), mar = c(5.1, 6.1, 4.1, 2.1), ask = TRUE) (u <- par("usr")) for(do.box in c(FALSE,TRUE)) { matplot(matrix(1:30,15,2), type="l", lty=1:2) par(mfg = c(2, 1, 2, 1)) print(u <- par("usr")) if(do.box) box() legend(u[1:2], u[3:4], c("a","b"), lty = 1:2, col=1:2, bty = "y") } ##--> The first time, the legend is not properly done; ## the second time, after "box()" it is! ---> You can use this as a workaround for the moment: Use box(col = 0) which gives an invisible box and makes subsequent legend(.) work okay! --- However, I assume you won't get what you want and what S-plus does: S: in legend(x,y,...) , if x & y of length 2, they specify opposite CORNERS whereas in R, they always specify just one point we have something like if(length(x) > 1) { x <- mean(x); y <- mean(y) ; ...... } That point is determined by xjust/yjust and defaults to the left-upper corner: In R, the size and placing of the legend's box is determined ``automagically'', using x.intersp, y.intersp, cex & text.width. Maybe we need a way to circumwent the automatic legend-box size computation... --- Martin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._