Plotting Tools

Statistical Computing, 36-350

Monday September 24, 2018

Last week: Data frames and apply

Part I

Plot basics

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
x = sort(runif(n, min=-2, max=2))
y = x^3 + rnorm(n)
plot(x, y)

Plot type

The type argument controls the plot type. Default is p for points; set it to l for lines

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

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

Try also b or o, for both points and lines


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 variable", ylab="My y variable")

Point type

Use the pch argument to control point type

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

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

Try also 20 for small filled circles, or "." for single pixels

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, lwd=1) # Solid line, default width

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


Use the col argument to control the color. Can be:

The function colors() returns a string vector of the available colors

plot(x, y, pch=19, col=1) # Black, default