Plots

Base R plots

Base R can draw quick plots with no packages at all. They are the fastest way to look at your data. (From Week 2 you’ll use {ggplot2} for figures you want to show other people.)

R Python (Matplotlib) Draws
plot(x, y) plt.scatter(x, y) a scatter plot of y against x
plot(x, y, type = "l") plt.plot(x, y) a line
hist(x) plt.hist(x) a histogram

Arguments set the labels: xlab and ylab label the axes and main sets the title.

The chunk below loads the data each time you open this page.

A scatter plot

Plot tmax_c (on the y axis) against tmin_c (on the x axis). What relationship do you expect?

plot(weather$tmin_c, weather$tmax_c)

Labels

Default axis labels such as weather$tmin_c are hard to read. Redraw the plot with proper labels, including units, by adding xlab and ylab arguments. Add a title with main.

plot(weather$tmin_c, weather$tmax_c,
     xlab = "Minimum temperature (°C)",
     ylab = "Maximum temperature (°C)",
     main = "Durham, monthly means, 1900–2025")

A histogram

hist() shows how the values of one variable are distributed. Draw a histogram of monthly rainfall, with a proper axis label.

hist(weather$rain_mm, xlab = "Monthly rainfall (mm)")

Bins

hist() chooses the number of bins for you, but the choice changes what you see. The breaks argument suggests a different number. Redraw the histogram with breaks = 5, then with breaks = 50. Which gives the most honest picture?

hist(weather$rain_mm, breaks = 5, xlab = "Monthly rainfall (mm)")
hist(weather$rain_mm, breaks = 50, xlab = "Monthly rainfall (mm)")

Too few bins hide the shape of the distribution; too many turn noise into spikes. There is no single right answer, which is why it is worth trying a few.

Extension: a warming trend?

Optional. Plot tmax_c for July only against year, using the square-bracket trick from the summary statistics tutorial. Add type = "l" to join the points into a line. What do you notice?

july <- weather$month == 7
plot(weather$year[july], weather$tmax_c[july], type = "l",
     xlab = "Year", ylab = "July maximum temperature (°C)")

Next topic