Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot got cut off after saving to file

Tags:

plot

r

legend

par

My fig has a large legend outside the plot. 6 lines with long description When I save it, the legend doesn't show up. I adjusted par, but it still doesn't work.

legend("topright", inset=c(-0.6,0),xpd=TRUE,cex=0.8,
+legend=c("A_all peaks","B_ from all peaks","C_from all peaks","A_from unique peaks",
+"B_from unique peaks","C_from unique peaks",
+"A_from overlap peaks","B_from overlap peaks","C_from overlap peaks"),
+col=c("green","red","blue","lightgreen","pink","lightblue","darkgreen","darkred","steelblue"),
+pch=c(20,20,20,20,20,20,20,20,20),bty="n")

> par()$oma
[1] 2 2 2 2 
> par()$mar
[1] 5.1 4.1 4.1 8.0

When save it with long width(tried 800,1000 pixel), no legend showed. But when as as short width(), part of legend shows. This is really confused me.first graph is 500*333, second graph is 500*800.

enter image description hereenter image description here

like image 664
lxcfuji Avatar asked Apr 17 '15 15:04

lxcfuji


2 Answers

Not sure how you're saving the plot to file, but my usual routine is to make a pretty plot in R by the usual means:

plot(blah,blah,blah)
legend(blah,blah,blah)

and then once I'm happy with the appearance of the figure the R console, I use pdf() or one of it's cousins(jpeg(),tiff(), etc.) to save it to file, making sure to set the width and height parameters like so:

# set up plotting device
pdf( {{FileName}}, 
   width = par('din')[1],
   height = par('din')[2])

plot(blah,blah,blah)
legend(blah,blah,blah)

# disconnect the plotting device
dev.off()
like image 197
Jthorpe Avatar answered Oct 29 '22 05:10

Jthorpe


Save it using png() or tiff():

tiff("filename",

<code for plot>,
height=5,width=7)
dev.off()
like image 33
brucezepplin Avatar answered Oct 29 '22 04:10

brucezepplin