Introduction

We are going to reproduce three different plots, of which we are only given the data set and the final image file. Each of them has two versions:

  • The first version is a plot made just using the layer functions of ggplot2 and leaving everything else with the defaults. This is, using ggplot, geom_*, stat_* and annotate_* functions (click on the image to see it bigger).

random image random image random image

  • The second version builds from the first plot to create a more complex display with customized scales_*, coord_*, facet_* or theme_*. Helper functions such as labs() or guides() could also be used (click on the image to see it bigger).

random image random image random image

library(ggplot2)

 : ggplot2 will automatically load all three data sets used in this practical.

Organization of the practical

You will see different icons through the document, the meaning of which is:

 : additional or useful information
 : a worked example
 : a practical exercise
 : a space to answer the exercise
 : a hint to solve an exercise
 : a more challenging exercise


Simple versions

Plot 1

# Dataset used: diamonds
  Answer:

 

Plot 2

# Dataset used: mpg

  Need help with the count numbers shown in each stacked bar? https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2

  Answer:

 

Plot 3

# Dataset: ToothGrowth
  Answer:

 


Complex versions

Plot 1

  Answer:

 

Plot 2

  Answer:

 

  This graph uses a scale_fill_brewer palette, can you find which one?

Plot 3

  Answer:

 

  This graph uses a scale_fill_brewer palette, can you find which one?