[R] line jump in plot legend title

David Winsemius dwinsemius at comcast.net
Mon Jul 18 21:47:32 CEST 2011


On Jul 18, 2011, at 1:14 PM, Dutrieux, Loïc wrote:

> Hello,
>
> In order to reduce the width of my legend in a plot I introduced  
> line jumps in the title. Here's the problem; the legend box hasn't  
> adapted accordingly and part of the title is printed out of the frame.
> See the example below:
>
> plot(1:10)
> legend("bottomright", bg="white", fill=c(7,8,12,13,19),  
> legend=c("7","8","12","13","19"), title="CAVM\nvegetation 
> \ncategories")
>
> Does anyone know a trick to cope with that problem?

It looks like this has been raised in the past, and reading the help  
page   makes me think no one has supplied a fix:

http://finzi.psych.upenn.edu/R/Rhelp02/archive/87940.html

(and looking at the code I think this is the area to start your  
hacking to make the box bigger:

else {
      h <- (n.legpercol + (!is.null(title))) * ychar + yc
# substitute 3 for the !is.null() call
      w0 <- text.width + (x.intersp + 1) * xchar
      if (mfill)
          w0 <- w0 + dx.fill
      if (do.lines)
          w0 <- w0 + (seg.len + x.off) * xchar
      w <- ncol * w0 + 0.5 * xchar
      if (!is.null(title) && (abs(tw <- strwidth(title, units = "user",
          cex = cex) + 0.5 * xchar)) > abs(w)) {
          xextra <- (tw - w)/2
          w <- tw
      }

And also adjust the positioning of the title text in this code

if (!is.null(title))
+             text2(left + w * title.adj, top - ymax, labels = title,
+                 adj = c(title.adj, 0), cex = cex, col = title.col)

... but I have not figured out which internal variable to adjust to  
push down the colored rectangles and legend labels.

Have you considered lattice or ggplot strategies.


>
> Thank you in advance,
>
> Regards,
>
> loïc
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list