[R] Please help with legend command

Jim Lemon jim at bitwrit.com.au
Thu Jul 5 14:09:18 CEST 2007


Smith, Phil (CDC/CCID/NCIRD) wrote:
> Hi R-ers:
> 
> I'm drawing a plot and have used different line types (lty) for
> different race/ethnicity groups. I want a legend that explains what line
> types correspond to the different race/ethnicity groups. I used the
> following code:
> 
> 
> legend( 1992 , 42  , c("Hispanic" , "non-Hispanic white (NHW)" ,
> "non-Hispanic black" , "AI/AN" , "Asian" ) , lty=1:5 ,cex = .6 , bty='n'
> )
> 
> Guess what? The legend "box" was so narrow that the line types that show
> up in that legend box look essentially the same, because they are short.
> I.e, although a line type might be a long dash followed by a short dash,
> only the long dash shows up in the box. The consequence of this is that
> the race/ethnic group that corresponds to the line type that is only a
> long dash cannot be distinguished from the legend.
> 
> How do I stretch that legend box out so as to allow lty to draw longer
> line segments?
> 
Hi Phil,
A quick hack is to get a copy of the function:

sink("newlegend.R")
legend
sink()

Get the file into a text editor and first get rid of the

<environment: namespace:graphics>

at the bottom, then change line 161 to:

w0 <- w0 + (4 + x.off) * xchar

and line 212 to:

seg.len <- 4

source the new function:

source("newlegend.R")

and your line segments will be twice as long.

Jim



More information about the R-help mailing list