[R] Resizing a graph with a legend in the outer margin - wish for functionality like mtext()

Peter Lomas peter.br.lomas at gmail.com
Wed Mar 25 19:21:49 CET 2015


Dear R-Helpers

I am attempting to get a legend in the outer margins of a graph that has a
similar flexibility to mtext(), within base graphics.  This is used in a
tool that will end up with graphs of many different coordinates, exported
in many different sizes.  I'm trying to come up with a general solution.
I've used "bottom right" before but I found it interferes with the graphs
frequently.

Here was my best attempt at something general, but then I encountered the
graph re-sizing issue:

x <- 1:100
y <- x +rnorm(100, 0,10)
plot(x,y, xlab="", main="Shrink and Enlarge This Graph")
abline(0,1 ,lty=2)

legend(x=grconvertX(1, from = "ndc", to = "user"),
       y=grconvertY(0, from = "ndc", to = "user"),
       lty=2,
       legend="The legend doesn't rescale nicely",
       xjust=1, yjust=0,
       bty="n",
       xpd=TRUE)

mtext("This example has some text in the outer margin.\nIt rescales
nicely.",
      side = 1, line=3.5, adj=0)


Thanks!
Peter

	[[alternative HTML version deleted]]



More information about the R-help mailing list