Department of Environmental Science, AUT

Basic Plotting: Prerequisites

Basic Plotting

Content you should have understood before watching this video:

  • Number 2, ‘Variables’
  • Number 3, ‘Variation in data’
  • Number 4, ‘Basic statistical metrics’
  • Number 5, ‘Standard deviation and standard error’
  • Number 8, ‘Confidence Intervals’

Basic Plotting: an Overview

Basic Plotting

Follow this flow chart to determine what kind of graph might be best suited for your data!

We will now show you how to produce these basic plots, and how to tweak them to make them prettier!

Scatter plots

Basic Plotting
par(mfrow = c(1, 2), mar = c(2.2, 2, 0.5, 1)) #plotting area and margins
plot(dist ~ speed, data = cars) #simple default plot
plot(dist ~ speed, data = cars, #customised plot (same data)
     ylab = "Distance (ft)",
     xlab = "Speed (mph)",
     xlim = c(0, 26), ylim = c(0, 122),
     xaxs = "i", yaxs = "i",
     mgp = c(1.2, .2, 0),
     cex.axis = 0.8,
     cex.lab = 1,
     tcl = 0.3,
     cex = 0.7,
     las = 1,
     pch = 16)

Line plots

Basic Plotting
par(mfrow = c(1, 2), mar = c(2.3, 2, 0.5, 1))
plot(circumference ~ age, data = Orange[Orange$Tree == 1, ], t = 'l')
plot(Orange$circumference[Orange$Tree == 1] ~ Orange$age[Orange$Tree == 1],
     t = 'b', ylim = c(0, 220), xlab = "Tree age (days)", ylab = "", yaxs = "i",
     mgp = c(1.3, .2, 0), cex.axis = 0.9, cex.lab = 1, tcl = 0.3, cex = 0.9,
     las = 1)
mtext("Tree diameter (mm)", side = 2, line = 1.7)

for (i in 2:5) {
  lines(Orange$circumference[Orange$Tree == i] ~ Orange$age[Orange$Tree == i],
        t = 'b', lty = i)}

Bar plots

Basic Plotting
par(mfrow = c(1, 2), mar = c(4.2, 2, 1, 1))
m = c(20, 30) #means per group, see chapter 2 on how to aggregate data sets
s = c(3, 4) #The standard errors per group
hh = barplot(m, ylim = c(0, 50), xlab = 'Treatment', ylab = 'Antibody (arbitrary units)',
             main = 'Drug test', names.arg = c('Drug', 'Control'))
segments(hh, m, hh, m+s)

barplot(as.numeric(AirPassengers)[1:60], main = 'International air passengers 1949-1953',
        ylab = 'Number of passengers \u00D7 1000', xlab = 'Time (months)', names.arg = 1:60)

Box plots

Basic Plotting

A simple exploratory plot. Check out ?boxplot to see what the box limits and antennas indicate!

boxplot(Petal.Length ~ Species, data = iris)

The most important in a nutshell

Basic Plotting
  • It’s important that you understand what plot is best suited to visualise your data (see flow chart)
  • For this, you have to have a good idea what your data set contains (be able to identify response and predictor variables)
  • Once you understand the basics, you can virtually google anything you might need to embellish your graphs!