Plotting data

Plotting of data in modern R is handled by ggplot2, which is installed as part of the tidyverse. There is an excellent cheat sheet and a great free online book, ggplot2: Elegant graphics for data analysis, by Hadley Wickham, Danielle Navarro, and Thomas Lin Pedersen.

We cannot cover all of ggplot2 in this workshop, so you are strongly encouraged to read the book and cheat sheet. For today, we will give a quick overview of how to create some simple graphs.

First we load the tidyverse in the same way as previously;

library(tidyverse)

Next we will load some data to plot. We will use the climate data from the previous section (available from cetml1659on.txt).

Plotting works best with tidy data, so we will load and tidy the data as in the previous section;

temperature <- read_table(
    "https://chryswoods.com/data_analysis_r/cetml1659on.txt",
    skip=6,
    na=c("-99.99", "-99.9"),
    col_types=cols("DATE"=col_integer())
)

month_levels <- c("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                  "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")

historical_temperature <- temperature %>%
    select(-YEAR) %>%
    pivot_longer(c("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                   "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"),
                 names_to="month",
                 values_to="temperature") %>%
    rename(year=DATE) %>%
    mutate(month=factor(month, month_levels))

Next, we will use ggplot to draw a graph (we will explain how this works after drawing).

ggplot(historical_temperature, aes(x = year, y = temperature)) + geom_point()

You should see a graph similar to this;