Plotting in R

Base R has a set of powerful plotting tools. An overview:

Scatter plot

To make a scatter plot of one variable versus another, use plot()

n = 50
set.seed(0)
x = sort(runif(n, min=-2, max=2))
y = x^3 + rnorm(n)
plot(x, y)

Type

The type argument controls the type. Default is p for points; set it to l for lines; b for both; or o for both, overplotted

plot(x, y, type="p")

plot(x, y, type="l")

plot(x, y, type="b")

plot(x, y, type="o")

Labels

The main argument controls the title; xlab and ylab are the x and y labels

plot(x, y, main="A noisy cubic") # Note the default x and y labels

plot(x, y, main="A noisy cubic", xlab="My x var", ylab="My y var")

Point type

Use the pch argument to control point type

plot(x, y, pch=21) # Empty circles, default

plot(x, y, pch=20) # Filled circles, small

plot(x, y, pch=19) # Filled circles, normal

Line type

Use the lty argument to control the line type, and lwd to control the line width

plot(x, y, type="l", lty=1) # Solid line, default

plot(x, y, type="l", lty=2) # Dashed line

plot(x, y, type="l", lty=3) # Dotted line

plot(x, y, type="l", lwd=3) # Solid line, 3 times as thick

Color

Use the col argument to control the color; can be 1 through 8 for basic colors, or a string for any of the 657 available named colors. The function colors() returns a string vector of the available colors

plot(1:9, 1:9, pch=19, col=1) # Black, default

plot(1:9, 1:9, pch=19, col=2) # Red

plot(1:9, 1:9, pch=19, col=1:9) # Color vector

plot(1:9, 1:9, pch=19, col="mediumorchid")

Multiple plots

To set up a plotting grid of arbitrary dimension, use the par() function, with the argument mfrow. Note: this will affect all following plots! (Except in separate R Markdown code chunks …)

par(mfrow=c(2,2)) # Grid elements are filled by row
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")

Margin

Default margins in R are large (and ugly); to change them, use the par() function, with the argument mar. Note: this will affect all following plots! (Except in separate R Markdown code chunks …)

par(mfrow=c(2,2), mar=c(4,4,2,0.5))
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")

Saving plots

Use the pdf() function to save a pdf file of your plot, in your R working directory. Use getwd() to get the working directory, and setwd() to set it

getwd() # This is where the pdf will be saved
## [1] "/Users/ryantibs/Dropbox/teaching/f16-350/lectures/plotting"
pdf(file="noisy_cubics.pdf", height=7, width=7) # Height, width are in inches
par(mfrow=c(2,2), mar=c(4,4,2,0.5))
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")
graphics.off()

Also, use the jpg() and png() functions to save jpg and png files