ggplot2

The basic plotting functions of R are nice to quickly visualize data, however, the strength of R is that it has many packages with extra functionality created by others that you can use. ggplot2 is one of those packages, created by Hadley Wickham. ggplot2 allows you to build more sophisticated plots in R using a “Grammar of Graphics” (therefore GGplot).

If ggplot2 is not yet available for you we need to install and load the ggplot2 library like so.

install.packages("ggplot2")
library(ggplot2)

ggplot2 is a very extensive package, so it is impossible to show all the possibilities during this course. If you would like to experiment further, the ggplot manual at http://ggplot2.tidyverse.org/reference/index.html is the place to go. A large variety of functionalities are listed there, including example plots.

Grammar of graphics

The grammar of graphics consists of a number of “layers”, which generate a ggplot when correctly put together. These five basic layers will be discussed here:

  • Data
  • Aesthetics
  • Geometries
  • Facets
  • Scales
  • Themes