4 Plotting and graphics

Sweet graphs, right? Want to learn how to make them? Okay, but baby steps here, alright?.


In this Chapter we will walk through plotting in R, both with the base graphic utilities that come with R, and the ggplot2 package from the tidyverse that has taken over the world (er, revolutionized how we write R code). Both of these are actually great to work with for different reasons. The base graphics are built-in, powerful, and give you 100% full control over your graphic environment. The ggplot2 library (and a million packages that people have written to work with it) takes these to a new level in terms of functionality and 95% of the time it will do exactly what you need. That other 5% of the time it is really great to be able to fall back on the base graphics.

For the examples in this chapter, we’ll work with the water quality data contained in physical.csv. You will need to download the class data sets that go with this book to play along if you have not already (click here for instructions from the course website). But, you should have downloaded those to complete the examples in Chapter 2.

We will walk through histograms, scatter plots, line graphs, and boxplots in base graphics and ggplot2 in this Chapter. Later in the book, we will add examples of how to plot predictions from statistical models alongside raw data using these same tools.

If you installed the tidyverse successfully in Chapter 3, then you can load all the packages we’ll need by including library(tidyverse) at the start of your script:

# Chapter 4 Lecture module

# Package load
library(tidyverse) 

# 4.2 Plotting with base R ----
# ...