Basic plotting with R
Overview
Teaching: 30 min
Exercises: 0 minQuestions
How to plot in R
Objectives
Learn using plotting tool in R
Learn to export graphical to file
Course content:
- Making exploratory graphs
- Principles of analytic graphics
- Plotting systems and graphics devices in R
- The base, lattice, and ggplot2 plotting systems in R
Exploratory graph
Why do we use graphs in data analysis?
- To understand data properties
- To find patterns in data
- To suggest modeling strategies
- To “debug” analyses
- To communicate results
Ploting System
There are 3 main plotting systems in R:
The Base plotting system
- Start with blank plot and build up the plot
- Plot is just a series of R command
- Flexible
The Lattice plotting system (using package::lattice)
- Plots created using single function call
- Good for putting many plots to screen
- Cannot add to plots once created
The ggplot plotting system (using package::ggplot2)
- Similar to Lattice but easier
- Many default mode
- Flexible between Base and Lattice
The Base plotting system
Important BASE graphic parameters
pch: the plotting symbol (default is open circle)
lty: the line type (default is solid line), can be dashed, dotted, etc.
lwd: the line width, specified as an integer multiple
col: the plotting color, specified as a number, string, or hex code; the colors() function gives you a vector of colors by name
xlab: character string for the x-axis label
ylab: character string for the y-axis label
Important BASE plotting function
plot: make a scatterplot, or other type of plot depending on the class of the object being plotted
lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
points: add points to a plot
text: add text labels to a plot using specified x, y coordinates
title: add annotations to x, y axis labels, title, subtitle, outer margin
mtext: add arbitrary text to the margins (inner or outer) of the plot
axis: adding axis ticks/labels
Motor Trend Car Road Tests example
The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).
- Usage:
data(mtcars) dim(mtcars) names(mtcars) head(mtcars) print(mtcars) - Simple Summaries of Data Simple Summaries of Data
Five-number summary
summary(mtcars) - Boxplots:
boxplot(mtcars$mpg,col="blue",main="Boxplot for mpg")factor(mtcars$cyl) boxplot(mpg~cyl,data=mtcars, col=terrain.colors(3),main="MPG by car cylinders", xlab = "cylinders",ylab="mpg") legend("topright",c("4","6","8"),fill = terrain.colors(3))
- Histograms
hist(mtcars$hp, col="magenta")
- Barplot
barplot(mtcars$mpg,col="green", main="MPG for 32 cars")
- Multiple historgrams
par(mfrow=c(1,2)) hist(mtcars$mpg,col="blue") hist(mtcars$wt,col="blue")
- Scatter plot
plot(mtcars$mpg,mtcars$wt,main="Car Fuel vs Weight", xlab="Milage per Gallon",ylab="Weight", col = mtcars$cyl,pch=16,cex=3) legend("topright",legend=c(8,6,4),pch=16,cex=3, col=c("grey","magenta","blue"))
Graphics Devices
A graphics device is something where you can make a plot appear When you make a plot in R, it has to be “sent” to a specific graphics device.
- A window on your computer (screen device): quick visualization
- A PDF, PNG, JPEG file (file device) #recommended for documents, paper, presentation
The most common place for a plot to be “sent” is the screen device
- On a Mac the screen device is launched with the quartz()
- On Windows the screen device is launched with windows()
- On Unix/Linux the screen device is launched with x11()
- save graphic to PDF
- save graphic to PNG, JPEG
dev.copy(png,"filename.png") # to save the image to file
dev.off() # to close all the graphical devices
Key Points
Plot