Chapter 7 Data visualization with ggplot2

7.0.0.1 Disclaimer

We will be using the functions in the ggplot2 package. R has powerful built-in plotting capabilities, but for this exercise, we will be using the ggplot2 package, which facilitates the creation of highly-informative plots of structured data.

7.0.0.1 Learning Objectives

  • Visualize some of the mammals data from Figshare surveys.csv
  • Understand how to plot these data using R ggplot2 package. For more details on using ggplot2 see official documentation.
  • Building step by step complex plots with the ggplot2 package

Load required packages

# plotting package
library(ggplot2)
# modern data frame manipulations
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

7.1 Plotting with ggplot2

We will make the same plot using the ggplot2 package.

ggplot2 is a plotting package that makes it simple to create complex plots from data in a dataframe. It uses default settings, which help creating 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 = surveys_complete)
  • define aesthetics (aes), that maps variables in the data to axes on the plot or to plotting size, shape color, etc.,
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length))
  • add geoms – graphical representation of the data in the plot (points, lines, bars). To add a geom to the plot use + operator:
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
  geom_point()

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
surveys_plot <- ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length))

# Draw the plot
surveys_plot + geom_point()

Notes:

  • Anything you put in the ggplot() function can be seen by any geom layers that you add. i.e. these are universal plot settings. This includes the x and y axis you set up in aes().
  • You can also specify aesthetics for a given geom independently of the aesthetics defined globally in the ggplot() function.

7.2 Building your plots iteratively

Building plots with ggplot is typically an iterative process. We start by defining the dataset we’ll use, lay the axes, and choose a geom.

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point()

Then, we start modifying this plot to extract more infromation from it. For instance, we can add transparency (alpha) to avoid overplotting.

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1)

We can also add colors for all the points

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1, color = "blue")

Or to color each species in the plot differently:

ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1, aes(color=species_id))

7.3 Boxplot

Visualising the distribution of weight within each species.

ggplot(data = surveys_complete, aes(x = species_id, y = hindfoot_length)) +
    geom_boxplot()

By adding points to boxplot, we can have a better idea of the number of measurements and of their distribution:

ggplot(data = surveys_complete, aes(x = species_id, y = hindfoot_length)) +
    geom_boxplot(alpha = 0) +
    geom_jitter(alpha = 0.3, color = "tomato")

Notice how the boxplot layer is behind the jitter layer? What do you need to change in the code to put the boxplot in front of the points such that it’s not hidden.

7.3 Challenges

Boxplots are useful summaries, but hide the shape of the distribution. For example, if there is a bimodal distribution, this would not be observed with a boxplot. An alternative to the boxplot is the violin plot (sometimes known as a beanplot), where the shape (of the density of points) is drawn.

  • Replace the box plot with a violin plot; see geom_violin()

In many types of data, it is important to consider the scale of the observations. For example, it may be worth changing the scale of the axis to better distribute the observations in the space of the plot. Changing the scale of the axes is done similarly to adding/modifying other components (i.e., by incrementally adding commands).

  • Represent weight on the log10 scale; see scale_y_log10()

  • Create boxplot for hindfoot_length.

7.4 Plotting time series data

Let’s calculate number of counts per year for each species. To do that we need to group data first and count records within each group.

yearly_counts <- surveys_complete %>%
                 group_by(year, species_id) %>%
                 tally

Timelapse data can be visualised as a line plot with years on x axis and counts on y axis.

ggplot(data = yearly_counts, aes(x = year, y = n)) +
     geom_line()

Unfortunately this does not work, because we plot data for all the species together. We need to tell ggplot to draw a line for each species by modifying the aesthetic function to include group = species_id.

ggplot(data = yearly_counts, aes(x = year, y = n, group = species_id)) +
    geom_line()

We will be able to distinguish species in the plot if we add colors.

ggplot(data = yearly_counts, aes(x = year, y = n, group = species_id, colour = species_id)) +
    geom_line()

7.5 Faceting

ggplot has a special technique called faceting that allows to split one plot into multiple plots based on a factor included in the dataset. We will use it to make one plot for a time series for each species.

ggplot(data = yearly_counts, aes(x = year, y = n, group = species_id, colour = species_id)) +
    geom_line() +
    facet_wrap(~ species_id)

Now we would like to split line in each plot by sex of each individual measured. To do that we need to make counts in data frame grouped by year, species_id, and sex:

 yearly_sex_counts <- surveys_complete %>%
                      group_by(year, species_id, sex) %>%
                      tally

We can now make the faceted plot splitting further by sex (within a single plot):

 ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = species_id, group = sex)) +
     geom_line() +
     facet_wrap(~ species_id)

Usually plots with white background look more readable when printed. We can set the background to white using the function theme_bw().

 ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = species_id, group = sex)) +
     geom_line() +
     facet_wrap(~ species_id) +
     theme_bw()

To make the plot easier to read, we can color by sex instead of species (species are already in separate plots, so we don’t need to distinguish them further).

ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex, group = sex)) +
    geom_line() +
    facet_wrap(~ species_id) +
    theme_bw()

7.6 Challenge

Use what you just learned to create a plot that depicts how the average weight of each species changes through the years.

The facet_wrap geometry extracts plots into an arbitrary number of dimensions to allow them to cleanly fit on one page. On the other hand, the facet_grid geometry allows you to explicitly specify how you want your plots to be arranged via formula notation (rows ~ columns; a . can be used as a placeholder that indicates only one row or column).

Let’s modify the previous plot to compare how the weights of male and females has changed through time.

## One column, facet by rows
yearly_sex_weight <- surveys_complete %>%
    group_by(year, sex, species_id) %>%
    summarize(avg_weight = mean(weight))
ggplot(data = yearly_sex_weight, aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
    geom_line() +
    facet_grid(sex ~ .)

# One row, facet by column
ggplot(data = yearly_sex_weight, aes(x=year, y=avg_weight, color = species_id, group = species_id)) +
    geom_line() +
    facet_grid(. ~ sex)

7.7 Customization

Take a look at the ggplot2 cheat sheet (https://www.rstudio.com/wp-content/uploads/2015/08/ggplot2-cheatsheet.pdf), and think of ways to improve the plot. You can write down some of your ideas as comments in the Etherpad.

Now, let’s change names of axes to something more informative than ‘year’ and ‘n’ and add a title to this figure:

ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex, group = sex)) +
    geom_line() +
    facet_wrap(~ species_id) +
    labs(title = 'Observed species in time',
         x = 'Year of observation',
         y = 'Number of species') +
    theme_bw()

The axes have more informative names, but their readibility can be improved by increasing the font size. While we are at it, we’ll also change the font family:

ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex, group = sex)) +
    geom_line() +
    facet_wrap(~ species_id) +
    labs(title = 'Observed species in time',
        x = 'Year of observation',
        y = 'Number of species') +
    theme_bw() +
    theme(text=element_text(size=16, family="Arial"))

After our manipulations we notice that the values on the x-axis are still not properly readable. Let’s change the orientation of the labels and adjust them vertically and horizontally so they don’t overlap. You can use a 90 degree angle, or experiment to find the appropriate angle for diagonally oriented labels.

ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex, group = sex)) +
    geom_line() +
    facet_wrap(~ species_id) +
    labs(title = 'Observed species in time',
        x = 'Year of observation',
        y = 'Number of species') +
    theme_bw() +
    theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
                        axis.text.y = element_text(colour="grey20", size=12),
          text=element_text(size=16, family="Arial"))

If you like the changes you created to the default theme, you can save them as an object to easily apply them to other plots you may create:

arial_grey_theme <- theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
                          axis.text.y = element_text(colour="grey20", size=12),
                          text=element_text(size=16, family="Arial"))
ggplot(surveys_complete, aes(x = species_id, y = hindfoot_length)) +
    geom_boxplot() +
    arial_grey_theme

With all of this information in hand, please take another five minutes to either improve one of the plots generated in this exercise or create a beautiful graph of your own. Use the RStudio ggplot2 cheat sheet, which we linked earlier for inspiration.

Here are some ideas:

After creating your plot, you can save it to a file in your favourite format. You can easily change the dimension (and its resolution) of your plot by adjusting the appropriate arguments (width, height and dpi):

my_plot <- ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex, group = sex)) +
    geom_line() +
    facet_wrap(~ species_id) +
    labs(title = 'Observed species in time',
        x = 'Year of observation',
        y = 'Number of species') +
    theme_bw() +
    theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
                        axis.text.y = element_text(colour="grey20", size=12),
          text=element_text(size=16, family="Arial"))
ggsave("name_of_file.png", my_plot, width=15, height=10)

7.8 Making Plots Interactive with Plotly

You can make plots interactive by simply passing the ggplot2 object to the plotly library’s ggplot2 function. This is most useful for dense plots in which you wish to inspect a plot’s individual elements interactively, such as outliers in a scatterplot.

This only works when outputting to HTML, since it takes advantage of the htmlwidgets framework.

First load the library:

library(plotly) # install.packages('plotly')
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:graphics':
#> 
#>     layout

Let’s start with the first ggplot of a scatterplot.

# get ggplot2 object for scatter plot
p <- ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
    geom_point(alpha = 0.1, aes(color=species_id))
  
# feed  ggplot2 object to plotly::ggplotly() to make interactive
ggplotly(p)

Running the code above from within RStudio’s Console, outputs to a Viewer pane, rather than the Plots pane of previous ggplot2 functions. Hovering over any given point displays the information associated with that data point.

Popup information on hover for scatterplot rendered through plotly::ggplotly()

Popup information on hover for scatterplot rendered through plotly::ggplotly()

You can similarly make any other ggplot2 interactive, such as for a box plot:

# box plot
p <- ggplot(data = surveys_complete, aes(x = species_id, y = hindfoot_length)) +
  geom_boxplot()
ggplotly(p)

Or for a line graph:

# line graph
p <- ggplot(yearly_counts,
            aes(x = year, y = n, group = species_id, colour = species_id)) +
  geom_line()
ggplotly(p)

For more options, please visit plot.ly/ggplot2.