Lesson 6. Plot data and customize plots with ggplot plots in R - earth analytics - data science for scientists


In this tutorial, we will explore more advanced plotting techniques using ggplot2.

Learning objectives

At the end of this activity, you will be able to:

  • Use the ggplot() plot function to create custom plots.
  • Add labels to x and y axes and a title to your ggplot() plot.
  • Customize the colors and look of a ggplot() plot.

What you need

You need R and RStudio to complete this tutorial. Also you should have an earth-analytics directory setup on your computer with a /data directory within it.

In our week 1 homework, we used the quick plot function of ggplot2 to plot our data. In this tutorial, we’ll explore ggplot - which offers many more advanced plotting features.

Let’s explore the code below to create a quick plot.


# load the ggplot2 library for plotting
library(ggplot2)

# download data from figshare
# note that we already downloaded the data to our laptops previously
# but in case you don't have it - re-download it by uncommenting the code below.
# download.file("https://ndownloader.figshare.com/files/9282364",
#              "data/boulder-precip.csv",
#              method = "libcurl")

# import data
boulder_precip <- read.csv(file="data/boulder-precip.csv")

# view first few rows of the data
head(boulder_precip)
##     X       DATE PRECIP
## 1 756 2013-08-21    0.1
## 2 757 2013-08-26    0.1
## 3 758 2013-08-27    0.1
## 4 759 2013-09-01    0.0
## 5 760 2013-09-09    0.1
## 6 761 2013-09-10    1.0

# when we download the data we create a dataframe
# view each column of the data frame using its name (or header)
boulder_precip$DATE
##  [1] "2013-08-21" "2013-08-26" "2013-08-27" "2013-09-01" "2013-09-09"
##  [6] "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13" "2013-09-15"
## [11] "2013-09-16" "2013-09-22" "2013-09-23" "2013-09-27" "2013-09-28"
## [16] "2013-10-01" "2013-10-04" "2013-10-11"

# view the precip column
boulder_precip$PRECIP
##  [1] 0.1 0.1 0.1 0.0 0.1 1.0 2.3 9.8 1.9 1.4 0.4 0.1 0.3 0.3 0.1 0.0 0.9
## [18] 0.1

# q plot stands for quick plot. Let's use it to plot our data
qplot(x=boulder_precip$DATE,
      y=boulder_precip$PRECIP)

quick plot of precip data

Plot with ggplot2

ggplot2 is a plotting package that makes it simple to create complex plots from data in a data.frame. It uses default settings, which help to create publication quality plots with a minimal amount of settings and tweaking.

ggplot graphics are built step by step by adding new elements.

To build a ggplot() we need to:

  • Bind the plot to a specific data frame using the data argument
ggplot(data = boulder_precip)

  • Define aesthetics (aes), by selecting the variables to be plotted and the variables to define the presentation such as plotting size, shape color, etc.,
ggplot(data = boulder_precip, aes(x = DATE, y = PRECIP))
  • Add geoms – graphical representation of the data in the plot (points, lines, bars). To add a geom to the plot use + operator:
ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
  geom_point()

ggplot boulder precip

The + in the ggplot2 package is particularly useful because it allows you to modify existing ggplot objects. This means you can easily set up plot “templates” and conveniently explore different types of plots, so the above plot can also be generated with code like this:

# Create the plot object (nothing will render on your screen)
precip_plot <-  ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP))

# Draw the plot
precip_plot + geom_point()

We can also apply a color to our points

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(color = "blue")

ggplot with blue points

And adjust the transparency

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(alpha=.5, color = "blue")

ggplot with blue points and alpha

Or to color each value in the plot differently

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(alpha = 0.9, aes(color=PRECIP))

ggplot with colored points

We can turn our plot into a bar plot

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_bar(stat="identity")

ggplot with bars

Turn the bar outlines blue

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_bar(stat="identity", color="blue")

ggplot with blue bars

Or change the fill to bright green

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_bar(stat="identity", color="blue", fill="green")

ggplot with green bars

Add plot labels

You can add labels to your plots as well. Let’s add a title, and x and y labels using the glab() argument.

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(alpha = 0.9, aes(color=PRECIP)) +
    labs(x="Date",
      y="Precipitation (Inches)",
      title="Daily Precipitation (inches)",
      subtitle="Boulder, Colorado 2013")

ggplot with labels

Finally, explore using themes

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(alpha = 0.9, aes(color = PRECIP)) +
    labs(x = "Date",
      y = "Precipitation (Inches)",
      title = "Daily Precipitation (inches)",
      subtitle = "Boulder, Colorado 2013") + theme_bw()

ggplot with labels and themes

Make font size for all labels larger by setting the base size!

ggplot(data = boulder_precip,  aes(x = DATE, y = PRECIP)) +
    geom_point(alpha = 0.9, aes(color = PRECIP)) +
    labs(x = "Date",
      y = "Precipitation (Inches)",
      title = "Daily Precipitation (inches)",
      subtitle = "Boulder, Colorado 2013") + theme_bw(base_size = 9)

ggplot with labels and themes

More on customizing your plots

There are many different tutorials out there on customizing ggplot plots. A few are listed below.

Optional challenge

Customize the plot that we created in last weeks homework as follows:

  • Change the colors of the plot
  • Plot the data using points geom_point() AND bars geom_bar()
  • Add a title to your plot and x and y axis labels.

Leave a Comment