Silvia Liverani
Home

Research

Blog

Programming

Teaching

Personal

Links

Contact


Main

My
software

LaTeX

Linux

C++

R


Notes on R

Notes on R
Pretty graphs in R

Check out the R Graph Gallery for ideas and examples of R graphics at their best.


How to run R from a Linux console

It may happen that you have a few lines of R code that read in a data file and produce a few graphs. (See here how to produce and save graphs in R automatically.) You might find useful to run your R code without having to open R, set the working directory, copying the R code in and running it. Here is how. Save your R code in a file called, say, my_code.R and then type in a Linux console: the following

R CMD BATCH my_code.R

This runs R in batch mode. Batch jobs are set up so that they can be run to completion without human interaction, so all input data is preselected through scripts or command-line parameters.


How to save R graphs in any format

This is very useful when plotting figures that need to be included in a LaTeX document. For example, pdflatex does not work with postscript figures: it works with jpg and pdf figures instead. So, here is the bitmap command in R which saves R plots in external files in all sorts of formats. For example,

bitmap("plot.jpg",type="jpeg",onefile=T,width=5,height=5,res=1000)
plot(x,y)
dev.off()

creates a file of type jpeg, named plot.jpg of a specific width and height (these two commands are especially useful when the plot is not squared) and of resolution 1000. Then run the plot command and once you are done plotting, close the plot.jpg file with dev.off().