line jump in plot legend title

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

line jump in plot legend title

loic
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?

Thank you in advance,

Regards,

loïc

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: line jump in plot legend title

David Winsemius

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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: line jump in plot legend title

loic
As suggested by David, I applied some modifications to the code of the legend function so that the legend box size adapts to the number of line jumps in the legend title.

Attached the modified code of the function.

Thanks to David

Regards,

Loïc
Wageningen University

legend.r
Reply | Threaded
Open this post in threaded view
|

Re: line jump in plot legend title

Duncan Murdoch-2
On 11-07-19 8:16 AM, loic wrote:
> As suggested by David, I applied some modifications to the code of the legend
> function so that the legend box size adapts to the number of line jumps in
> the legend title.
>
> Attached the modified code of the function.

No code was attached.

In case you didn't, could you make sure that your modification is to the
version of the code that's in the svn repository?  Then it will be
straightforward to import your change into R.

That version is online at

https://svn.r-project.org/R/trunk/src/library/graphics/R/legend.R

if your changes only affect the legend() function.

Duncan Murdoch

>
> Thanks to David
>
> Regards,
>
> Loïc
> Wageningen University
>
> http://r.789695.n4.nabble.com/file/n3677996/legend.r legend.r
>
> --
> View this message in context: http://r.789695.n4.nabble.com/line-jump-in-plot-legend-title-tp3676157p3677996.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: line jump in plot legend title

David Winsemius

On Jul 19, 2011, at 10:38 AM, Duncan Murdoch wrote:

> On 11-07-19 8:16 AM, loic wrote:
>> As suggested by David, I applied some modifications to the code of  
>> the legend
>> function so that the legend box size adapts to the number of line  
>> jumps in
>> the legend title.
>>
>> Attached the modified code of the function.
>
> No code was attached.

But there was code at the link. It does behave as hoped with defaults  
unchanged, but when cex is altered, the size of the box does not seem  
to change appropriately.

>
> In case you didn't, could you make sure that your modification is to  
> the version of the code that's in the svn repository?  Then it will  
> be straightforward to import your change into R.
>
> That version is online at
>
> https://svn.r-project.org/R/trunk/src/library/graphics/R/legend.R
>
> if your changes only affect the legend() function.
>
> Duncan Murdoch
>
>>
>> Thanks to David
>>
>> Regards,
>>
>> Loïc
>> Wageningen University
>>
>> http://r.789695.n4.nabble.com/file/n3677996/legend.r legend.r
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/line-jump-in-plot-legend-title-tp3676157p3677996.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: line jump in plot legend title

loic
I also noticed, after sending the code that the modification does only work when the legend is positioned at the bottom of the figure region , and the function crashes when no title is provided.
The modification I applied to the code was intended to be more the solving of a particular problem than a real contribution to the function. Unfortunately my programing skills are quite limited to really contribute to the project.
However, if one of you manage to make the change work in all case, I believe including it in a next version of R is relevant.

Regards,

Loïc

-----Original Message-----
From: David Winsemius [mailto:[hidden email]]
Sent: dinsdag 19 juli 2011 16:56
To: Duncan Murdoch
Cc: Dutrieux, Loïc; [hidden email]
Subject: Re: [R] line jump in plot legend title


On Jul 19, 2011, at 10:38 AM, Duncan Murdoch wrote:

> On 11-07-19 8:16 AM, loic wrote:
>> As suggested by David, I applied some modifications to the code of
>> the legend function so that the legend box size adapts to the number
>> of line jumps in the legend title.
>>
>> Attached the modified code of the function.
>
> No code was attached.

But there was code at the link. It does behave as hoped with defaults unchanged, but when cex is altered, the size of the box does not seem to change appropriately.

>
> In case you didn't, could you make sure that your modification is to
> the version of the code that's in the svn repository?  Then it will be
> straightforward to import your change into R.
>
> That version is online at
>
> https://svn.r-project.org/R/trunk/src/library/graphics/R/legend.R
>
> if your changes only affect the legend() function.
>
> Duncan Murdoch
>
>>
>> Thanks to David
>>
>> Regards,
>>
>> Loïc
>> Wageningen University
>>
>> http://r.789695.n4.nabble.com/file/n3677996/legend.r legend.r
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/line-jump-in-plot-legend-title-tp367615
>> 7p3677996.html Sent from the R help mailing list archive at
>> Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.