Plotting Tools

Statistical Computing, 36-350

Tuesday October 12, 2021

Last week: Text manipulation

Part I

Plot basics

Plotting in R

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

The ggplot2 package also provides very nice (and very different) plotting tools; we won’t cover it in this course (it tends to be the focus in Statistical Graphics, 36-315)

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)

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

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 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

Color

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