Skip to content

Saving R Graph to a file

8 messages · frespider, John, Robert Baer +2 more

#
Hi

I am not sure why I can't get my plot saved to a file as .ps, I searched
online and I found that I have to use something is called postscript,png or
pdf function which I did but still not working.

Actually what I have is a matrix with almost 300-400 columns. I need to
create a histogram and boxplot for some columns as .ps file (with reasonable
size if i can adjust that would be nice also) so I can import them in my
latex code to display a good chart on my report.  And I found out R display
a certain limit of device. 
Can you please help me code this?

This an example I create 
data(CO2)
png(filename="C:/R/figure.png", height=295, width=300,  bg="white")
hist(CO2[,4])
device.off()
pdf(filename="C:/R/figure.pdf", height=295, width=300,  bg="white")
hist(CO2[,4])
postscript(filename="C:/R/figure.pdf", height=295, width=300,  bg="white")
hist(CO2[,4])


Thanks 





--
View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.html
Sent from the R help mailing list archive at Nabble.com.
#
On Sun, Nov 4, 2012 at 4:16 AM, frespider <frespider at hotmail.com> wrote:
Did you try running your own code? It should have thrown an error here
since there's no device.off() function.

You just want dev.off()

Michael
#
On Sat, 3 Nov 2012 21:16:49 -0700 (PDT)
frespider <frespider at hotmail.com> wrote:
You're leaving out some critical information like the error messages, if
any, you receive.  That said, your example won't work properly because
it is not properly coded.

FI'm sure it gets tiresome being told to read the manual, but that is
the way you learn R.  For poscript output you need to read
"?postscript()" and follow the information there.  There are important
differences in detail between png(), pdf(), and postscript() that are
critical. CAREFULLY read the help for postscript(), and also check into
dev.off() use too. Your example doesn't restore things properly.  If
you do the above, you'll discover what is wrong with your example.


JWDougherty
#
Some hints:
For pdf(), height and width are in inches, not pixels.  dev.off() is 
necessary after drawing the image for pdf(). The name for the file 
argument (file="c:/figure.xxx") is file not filename

hist(CO2[,5]) is more interesting

And yes,
?pdf
?postscript
?ping
On 11/3/2012 11:16 PM, frespider wrote:

  
    
#
On 11/4/2012 4:32 AM, Robert Baer wrote:
?png

  
    
#
Hello,

#Example 1: The following code to save svyboxplots works for me

pdf("boxplots_dthage.pdf", width = 1020) # 4 boxplots in 2 columns and 2 rows
par(mfrow=c(2,2),  oma=c(0,0,0,0))
# svyboxplot commands not shown
dev.off()


#Example 2: The following code to save a ggplot graph works for me:
# ggolot () not shown
print (p)
ggsave(file='Xfacet_abodill_age3.pdf', width=12, height=8) 


Thanks,

Pradip Muhuri
#
On Sun, Nov 4, 2012 at 1:18 PM, frespider <frespider at hotmail.com> wrote:
Beyond the ones in the relevant help files,

png()
plot(1:5)
dev.off()

should make a plot.

Michael