These exercises require you to generate plots of various kinds. This document shows both the plots that you should obtain.

Not everything was necessarily covered during the teaching session, you may need to use R help pages, the Cheat Sheets, or search the web to figure out the answers.

Part I – geoms and aesthetics

These first few exercises will run through some of the simple principles of creating a ggplot2 object, assigning aesthetics mappings and geoms.

  1. Read in the cleaned patients dataset, patient-data-cleaned.txt, into a new object called patients.
## # A tibble: 100 x 15
##    ID    Name  Sex   Smokes Height Weight Birth      State Grade Died 
##    <chr> <chr> <chr> <chr>   <dbl>  <dbl> <date>     <chr> <dbl> <lgl>
##  1 AC/A… Mich… Male  Non-S…   183.   76.6 1972-02-06 Geor…     2 FALSE
##  2 AC/A… Derek Male  Non-S…   179.   80.4 1972-06-15 Colo…     2 FALSE
##  3 AC/A… Todd  Male  Non-S…   169.   75.5 1972-07-09 New …     2 FALSE
##  4 AC/A… Rona… Male  Non-S…   176.   94.5 1972-08-17 Colo…     1 FALSE
##  5 AC/A… Chri… Fema… Non-S…   164.   71.8 1973-06-12 Geor…     2 TRUE 
##  6 AC/A… Dana  Fema… Smoker   158.   69.9 1973-07-01 Indi…     2 FALSE
##  7 AC/A… Erin  Fema… Non-S…   162.   68.8 1972-03-26 New …     1 FALSE
##  8 AC/A… Rach… Fema… Non-S…   166.   70.4 1973-05-11 Colo…     1 FALSE
##  9 AC/A… Rona… Male  Non-S…   181.   76.9 1971-12-31 Geor…     1 FALSE
## 10 AC/A… Bryan Male  Non-S…   167.   79.1 1973-07-19 New …     2 FALSE
## # … with 90 more rows, and 5 more variables: Count <dbl>,
## #   Date.Entered.Study <date>, Age <dbl>, BMI <dbl>, Overweight <lgl>

Scatterplots

  1. Generate a scatter plot of BMI versus Weight using the patient dataset .

  1. Extending the plot from exercise 2, add a colour scale to the scatterplot based on the Height variable.

  1. Using an additional geom, add an extra layer of a fit line to the solution from exercise 3.

  1. Does the fit in question 5 look good? Look at the help page for geom_smooth and adjust the method to fit a straight line without standard error bounds.

Boxplots and Violin plots

  1. Generate a boxplot of BMIs comparing smokers and non-smokers.

  1. Following from the boxplot comparing smokers and non-smokers in exercise 6, colour boxplot edges by Sex.

  1. Produce a similar boxplot of BMIs but this time group data by Sex and colour the interior of the box (not the outline) by Age.

Hint: is Age currently the right type of variable for grouping in a box plot?

  1. Regenerate the solution to exercise 8 but this time using a violin plot.

Histogram and Density plots

  1. Generate a histogram of BMIs with each bar coloured blue, choosing a suitable bin width.

  1. Instead of a histogram, generate a density plot of BMI

  1. Generate density plots of BMIs coloured by Sex.

Hint: alpha can be used to control transparency.

Line plots - time series data

Time series data is often represented using line graphs. Here we will look at the data in the diabetes.txt file.

  1. Read in the cleaned patients dataset, diabetes.txt, into a new object called diabetes. There are lots of patients in this study, let’s just look at a few of them - subset the table to only keep patients with ID’s AC/AH/001, AC/AH/017, and AC/AH/020.
## # A tibble: 45 x 4
##    ID        Date       Glucose    BP
##    <chr>     <date>       <dbl> <dbl>
##  1 AC/AH/001 2011-03-07     100    98
##  2 AC/AH/001 2011-03-14     110    89
##  3 AC/AH/001 2011-03-24      94    88
##  4 AC/AH/001 2011-03-31     111    92
##  5 AC/AH/001 2011-04-03      94    83
##  6 AC/AH/001 2011-05-21     110    93
##  7 AC/AH/001 2011-06-24     105    79
##  8 AC/AH/001 2011-07-11      88    86
##  9 AC/AH/001 2011-07-11     101    92
## 10 AC/AH/001 2011-07-13     112    88
## # … with 35 more rows
  1. Create a line plot that allows us to examine the change in Glucose levels for each patient by Date.

  1. Add points to the plot from 14 to make the measurement time-points clearer. Instead of a dot change the shape to a hollow circle and colour the interior of the point.