Introduction to R: Plotting
One of the best reasons to use R is its ability to make graphics. There are some packages that you can install that can make really beautiful graphics, like ‘ggplot2.’ (Note that because R is open-source, anyone can contribute code for carrying out new operations. Bundles of code created for a particular purpose are called packages. Many packages are natively installed when R is installed, but many, like ‘ggplot2’, must be custom installed using the function ‘install.package’.)
Here we will focus on using the basic ‘plot’ command. Run ‘?plot’ to get more information about the arguments of this function. The only things that you need for doing a basic plot are a series of x coordinates and a series of y coordinates.
x = seq(0, 1, 0.01)
y = 5*sqrt(x) # compute y
plot(x, y)
By default, R plots points as open circles. If you want to plot a line, you just need to specify the argument ‘type’ in the ‘plot’ function.
plot(x, y, type='l')
You can also do things like change the x and y axis labels, change the color and type of line that is plotted, or add a main title. See ‘?plot’ for more details.
# Plot a thicker, dashed blue line
plot(x, y, type='l', lwd=2, lty=2, col=4, xlab='Detailed x label', ylab='Detailed y label', main='Plot')
You are also free to call functions from inside the ‘plot’ command.
logistic.map = function(n) {
n1 = n * (1 - n)
return(n1)
}
n = seq(0, 1, 0.01)
# Here, I manually set the range of the y axis to run from 0 to 1
plot(n, logistic.map(n), type='l', xlab='Pop. size at t', ylab='Pop. size at t+1', ylim=c(0, 1))
If you want to put multiple plots on the same graph, you can do that easily using the ‘lines’ function.
x = seq(0, 1, 0.01)
y1 = 4*x^3 - 6*x^2 + 3*x + 5
y2 = 5*x^3 - 7*x^2 + 3*x + 5
plot(x, y1, type='l', lwd=2, xlab='x values', ylab='y values')
lines(x, y2, lty=2, col=2, lwd=2) # change the color and line type so you can figure out which line is which
# use the legend command to add a legend
legend(x='topleft', # you can use 'topright', 'bottomleft', 'bottomright' for this, or you can specify x and y coordinates
legend=c('y1', 'y2'), #legend label
col = c(1, 2), # the color of lines in the legend
lty = c(1, 2), # one solid and one dashed line
bty='n' # don't put a box around the legend
)
It will often happen that one of the lines doesn’t fit on the graph because it has a larger (or smaller) range than the other lines. For example,
x = seq(0, 1, 0.01)
y1 = 4*x^3 - 6*x^2 + 3*x + 5
y2 = 5*x^3 - 7*x^2 + 3*x + 5
y3 = 7 * x
plot(x, y1, type='l', lwd=2, xlab='x values', ylab='y values')
lines(x, y2, lty=2, col=2, lwd=2)
lines(x, y3, lty=3, col=4, lwd=2)
legend(x='topleft', legend=c('y1', 'y2', 'y3'), col = c(1, 2, 4), lty = c(1, 2, 3), bty='n', lwd=2)
You can adjust the y limits to take account of the full range of values that you want to plot by combining all three sets of y values into one very long vector using ‘c’ and then calling the ‘range’ function, which computes the minimum and maximum values in a vector.
plot(x, y1, type='l', lwd=2, xlab='x values', ylab='y values', ylim=range(c(y1,y2,y3)))
lines(x, y2, lty=2, col=2, lwd=2)
lines(x, y3, lty=3, col=4, lwd=2)
legend(x='topleft', legend=c('y1', 'y2', 'y3'), col = c(1, 2, 4), lty = c(1, 2, 3), bty='n', lwd=2)