Ranjan Maitra
2014-Mar-03 21:02 UTC
[R] ggplot2: how to increase size of legend box when converting to pdf
Hi,
I have struggled with this quite a bit (all morning and afternoon) and I
am not sure I am any close to a solution. Basically, here is my sample
code:
require(grid)
require(ggplot2)
X <- data.frame(x = c(rnorm(10), rnorm(10, mean = 2, sd = 2), rnorm(10,
mean =5)), group = c(rep("Ellipse", 10), rep("PeakAmp", 10),
rep
("Rectangle", 10)))
bp <- ggplot(data=X, aes(x=group, y=x, colour=group)) + geom_point() +
theme_bw()
bp + theme(legend.background = element_rect(fill = "white", color
"black", size = 0.1, linetype="solid"),
legend.key element_rect(fill = 'white', color = "white", size
= 0.1),
legend.text = element_text(size = 8),
legend.key.height = unit(0.5, "cm"),
legend.key.width = unit(0.5, "cm"),
legend.justification=c(0.9,0.1), legend.position=c(0.9,
0.1))
dev.copy2pdf(file = "Rplots.pdf", height = 4, width = 6.5)
I get exactly what i want on the plotting device (x11). However, the
pdf has text spilling out of the legend box. I understand that this
could be an issue of the viewer and the encoding but is there anything
I can do in R to reliably get around this issue in the plots
preparation stage?
Many thanks and best wishes,
Ranjan
____________________________________________________________
Protect your computer files with professional cloud backup.
Get PCRx Backup and upload unlimited files automatically.
Learn more at http://backup.pcrx.com/mail
Duncan Murdoch
2014-Mar-03 21:17 UTC
[R] ggplot2: how to increase size of legend box when converting to pdf
On 14-03-03 4:02 PM, Ranjan Maitra wrote:> Hi, > > I have struggled with this quite a bit (all morning and afternoon) and I > am not sure I am any close to a solution. Basically, here is my sample > code: > > require(grid) > require(ggplot2) > > X <- data.frame(x = c(rnorm(10), rnorm(10, mean = 2, sd = 2), rnorm(10, > mean =5)), group = c(rep("Ellipse", 10), rep("PeakAmp", 10), rep > ("Rectangle", 10))) > > > > bp <- ggplot(data=X, aes(x=group, y=x, colour=group)) + geom_point() + > theme_bw() > > bp + theme(legend.background = element_rect(fill = "white", color > "black", size = 0.1, linetype="solid"), legend.key > element_rect(fill = 'white', color = "white", size = 0.1), > legend.text = element_text(size = 8), > legend.key.height = unit(0.5, "cm"), > legend.key.width = unit(0.5, "cm"), > legend.justification=c(0.9,0.1), legend.position=c(0.9, > 0.1)) > > dev.copy2pdf(file = "Rplots.pdf", height = 4, width = 6.5) > > > I get exactly what i want on the plotting device (x11). However, the > pdf has text spilling out of the legend box. I understand that this > could be an issue of the viewer and the encoding but is there anything > I can do in R to reliably get around this issue in the plots > preparation stage?Don't use dev.copy2pdf. Use pdf() to open a PDF device at the beginning, plot to it, then call dev.close(). This will do all the size calculations using the pdf device. It's possible your text will still not fit; in that case, something in your system is not measuring it properly, and there's not much R can do to fix it. Duncan Murdoch