R Graph Commands for Data Analysis

This post is notes from the Coursera Data Analysis Course.

Here are some basic R commands for creating some graphs.

Exploratory Graphs


boxplot
barchart
hist
plot
density

Final Graphs for a report

Final graphs need to look a little nicer. They must also have informative labels and a title and possibly a legend.

plot(data$column1, data$column2, pch=19, col='blue', cex=0.5,
xlab='X axis label', ylab='Y axis label', main='Title of Graph',
cex.lab=2, cex.axis=1.5)

legend(100,200, legend='Legend Info', col='blue', pch=19, cex=0.5)

Multipanels

It is often useful to display more than one graph at a time. Here is some code to display 2 graphs horizontally on the same panel.

par(mfrow=c(1,2))
plot(data$column1, data$column2)
plot(data$column3, data$column4)

Figure Captions


mtext(text='some caption')

Create a PDF


pdf(file='myfile.pdf',height=4,width=8)
par(mfrow=c(1,3))
hist(...)
mtext(text='caption',side=3,line=1)
plot(...)
mtext(...)
boxplot(...)
mtext(...)
dev.off()

A very similar thing can be done for PNG image files. Just use png() at the beginning instead.
Use dev.copy2pdf(file=’myfile.pdf’) to save an existing graph to a file.

2 thoughts on “R Graph Commands for Data Analysis”

Leave a Reply

Your email address will not be published. Required fields are marked *