In this computer lab, you will learn how to display the daya with graphs.

Basic R graphs

Make sure to start by loading the data from the previous lab into your R session:

t <- read.csv("data/2020-12-weather.csv", comment.char="#")

The first graph that we made above is a bit rough… So, let’s make it better.

  1. Adding some labels on the axes:
plot(t$temperature_avg~t$wind_max, xlab="Wind speed", ylab="Temperature", main="Correlation between wind and temperature.")

  1. Using a different shape for the points (not really an improvement, just showing that this option exists):
plot(t$temperature_avg~t$wind_max, xlab="Wind speed", ylab="Temperature", main="Correlation between wind and temperature.", pch=22) # Changing the shape of the points

List of all possible shapes (from https://rgraphics.limnology.wisc.edu/pch.php):

  1. Changing the color of the points:
plot(t$temperature_avg~t$wind_max, xlab="Wind speed", ylab="Temperature", main="Correlation between wind and temperature.", pch=22, col="red", bg="steelblue")

  1. Adding the linear regression line to the graph:
plot(t$temperature_avg~t$wind_max, xlab="Wind speed", ylab="Temperature", main="Correlation between wind and temperature.", pch=22, col="red", bg="steelblue")
abline(lm(t$temperature_avg~t$wind_max), col="red")

More advanced graphs: ggplot

We will now use the package ggplot2 (See: https://ggplot2.tidyverse.org/) to generate some much nicer looking graphs.

Quoting directly from the ggplot2 official documentation:

It’s hard to succinctly describe how ggplot2 works because it embodies a deep philosophy of visualisation. However, in most cases you start with ggplot(), supply a dataset and aesthetic mapping (with aes()). You then add on layers (like geom_point() or geom_histogram()), scales (like scale_colour_brewer()), faceting specifications (like facet_wrap()) and coordinate systems (like coord_flip()).

To produce a similar plot (temperature vs wind speed) with ggplot, the syntax would be:

library(ggplot2)

gp <- ggplot(t, aes(wind_max, temperature_avg)) +
  geom_point()

plot(gp)

A more complex example:

So far, it does not look much nicer than our previous graph. But let’s look at a slightly more complex example. Download the file weather-two-stations.csv which contains data from two weather stations. Our goal is to display the correlation (or absence of) between wind speed and temperature for two weather stations on the same graph.

t <- read.csv("data/weather-two-stations.csv")
gp <- ggplot(t, aes(wind_high, temp_avg, colour = Station, shape = Station)) +
  geom_point() +
  xlab("Wind speed") +
  ylab("Average temperature") +
  ggtitle("Comparing two stations")

plot(gp)

Scatter plots are not the only type of graphs that can be made with ggplot! For example, we could make a histogram:

gp <- ggplot(t, aes(x = wind_high)) +
  geom_histogram()
plot(gp)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

How about a histogram showing the distribution of wind speed for the two different stations?

gp <- ggplot(t, aes(x = wind_high, fill = Station, colour = Station)) +
  geom_histogram(alpha = 0.3, aes(y = ..density..), position = 'identity', bins = 15)
plot(gp)

Interactive graphs:

We can even make interactive graphs with the plotly package:

library(ggplot2)
library(plotly)
#library(gapminder)

gp <- ggplot(t, aes(wind_high, temp_avg, colour = Station, shape = Station)) +
  geom_point() +
  xlab("Wind speed") +
  ylab("Average temperature") +
  ggtitle("Comparing two stations")

ggplotly(gp, tooltip = c("wind_high", "temp_avg", "colour"))

Rmarkdown

R Markdown is a notebook interface, allowing you to combine narrative text and code together with the code output. Results can be exported to multiple formats, including HTML (web page), PDF, Powerpoint and MS Word.

To start a new R Markdown document in RStudio, go to File -> New File -> R Markdown

For this class, we will only use simple Document, with an HTML output (but feel free to explore the other options):

Here is the general structure of R Markdown documents:

# Big title
Some text to describe the analysis being performed.
```{r}
[Insert R code here]
```

You can find more information about R Markdown here: https://rmarkdown.rstudio.com/lesson-1.html and here is the R Markdown cheat sheet: https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf

Mini exercise:

Write an R Markdown document to generate the interactive graph from this page in an HTML document.

This concludes our computer lab on graphing data in R.