An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110718/4e75c078/attachment.pl>
line jump in plot legend title
6 messages · Duncan Murdoch, David Winsemius, Loïc Dutrieux
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
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 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.
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.
______________________________________________ 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.
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.
______________________________________________ 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.
______________________________________________ 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
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:dwinsemius at comcast.net] Sent: dinsdag 19 juli 2011 16:56 To: Duncan Murdoch Cc: Dutrieux, Lo?c; r-help at r-project.org 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.
______________________________________________ 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.
______________________________________________ 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