Back to Homepage

Visualizing Data

Tutorial Objectives

The objectives of this tutorial are to:

  • Learn how to make common data visualisations in R using ggplot2, including how to:
    • Make a scatterplot
      • Add a line of best fit to a scatterplot
      • Add categorical information to a scatterplot
      • Create a set of related scatterplots using facet_wrap()
    • Make a boxplot
      • add additional data to a boxplot
      • add color to a boxplot
    • Make a bar chart
      • add color to a bar chart
      • add additional data to a bar chart

Getting started

One particularly powerful aspect of R is that it enables one visualize data in a variety of ways. Additionally, it gives users a wide variety of customization options. We will work below on a number of common data visualizations. But rememeber, once you get the hang of the basics, the sky is the limit! Note that this tutorial is a simplified version of [this one] (https://r4ds.had.co.nz/data-visualisation.html). For more details on what is possible, check out the source webpages!

To get started, lets load ggplot and look at one of the datasets that comes with ggplot:

library(ggplot2)
summary(mpg)
##  manufacturer          model               displ            year     
##  Length:234         Length:234         Min.   :1.600   Min.   :1999  
##  Class :character   Class :character   1st Qu.:2.400   1st Qu.:1999  
##  Mode  :character   Mode  :character   Median :3.300   Median :2004  
##                                        Mean   :3.472   Mean   :2004  
##                                        3rd Qu.:4.600   3rd Qu.:2008  
##                                        Max.   :7.000   Max.   :2008  
##       cyl           trans               drv                 cty       
##  Min.   :4.000   Length:234         Length:234         Min.   : 9.00  
##  1st Qu.:4.000   Class :character   Class :character   1st Qu.:14.00  
##  Median :6.000   Mode  :character   Mode  :character   Median :17.00  
##  Mean   :5.889                                         Mean   :16.86  
##  3rd Qu.:8.000                                         3rd Qu.:19.00  
##  Max.   :8.000                                         Max.   :35.00