[R] legend, "lheight", and alignment

Gabor Grothendieck ggrothendieck at gmail.com
Thu Dec 1 22:53:36 CET 2011


On Thu, Dec 1, 2011 at 11:54 AM, emorway <emorway at usgs.gov> wrote:
> Hello,
>
> A bit of fairly simple code, yet I don't seem to be able to manipulate it
> quite as much as I would like:
>
> 1)  It would be nice if the objects appearing in the legend were aligned,
> and by aligned I mean the boxes are centered over the lines.  Do I need to
> adjust the use of "NA" in the code below to accomplish this?  Here's how it
> appears on my machine:
>
> http://r.789695.n4.nabble.com/file/n4129402/example.png
>
> 2)  Because I'm using a call to 'expression' in the text contained in the
> legend, it would be nice to space  the lines of text a bit more.  My feeble
> attempt was to increase the lheight parameter in the hopes this would affect
> the legend...to no avail.  Further, lheight cannot be added to the argument
> list of legend.  I've been unable to track down another parameter applicable
> to legend, suggestions very much appreciated.
>
> par(lheight=2)
> plot(1,1,col="white")
>
> legend("center", legend=c(expression(paste("Observed
> ",italic(bar(EC)[e]))),expression(paste("Simulated
> ",italic(bar(EC)[e]))),"test1","test2"),
>       lty = c(NA,NA,1,1),
>       col = c("black","black","red","blue"),
>       density = c(NA,25,NA,NA),
>       border=c("black","black",NA,NA),
>       fill = c("grey","black",NA,NA),
>       angle = c(NA,45,NA,NA),
>       cex = 1.25, bty = "n", xpd = TRUE)
>

For #1 it would be easier to just use 2 columns so that there is no
question of alignment in the first place, i.e. ncol = 2.  For #2 try
drawing the legend from lower level functions such as text since text
does respect lheight.  See the source of legendg in plotrix for how to
do that.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list