Preparing plots in R for statistical reports: a tutorial
Section 1 Introduction
This tutorial is written for students on MAS6005, but may be of use to students on other modules. The aim of this tutorial is not to show you how to make plots in R, rather, it is to get you thinking about how you might improve your plots. Minimal plotting commands in R typically will not produce something suitable for a report, so there will always be some tidying to do, but sometimes, you can go a little further in making your plots more informative to the reader.
1.1 Overview of this tutorial
You should first try the short exercise in Section 2. Some plots will be shown, the purpose of the plot will be stated, and you should think about how each plot could be improved. We’ll discuss each plot over the following sections
We’ll either be using built-in datasets, or datasets available in the package
MAS6005. This package isn’t on CRAN, but is available on github. Install the
devtools package if you need to, and then run the command.
1.3 Graphics packages
In this tutorial we’ll be using base graphics: the plotting tools available in the base installation of R. An alternative, and popular system for graphics is the
ggplot2 package (Wickham 2009).
ggplot2 can be a little more difficult to use, but can produce nicer-looking plots, and if you’re searching for help online on how to do a particular plot, it’s likely someone will have provided a solution using
ggplot2. A short intro to
ggplot2 is given at the end, together with code for producing all the plots presented in this tutorial. We’ll also very briefly look at
plotly (Sievert et al. 2017), which can be used for producing interactive graphics. (Of course, that’s not actually useful for written reports, but it’s a fun package to use, and may be useful either in your own analyses, or when presenting data online.)
Wickham, Hadley. 2009. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. http://ggplot2.org.
Sievert, Carson, Chris Parmer, Toby Hocking, Scott Chamberlain, Karthik Ram, Marianne Corvellec, and Pedro Despouy. 2017. Plotly: Create Interactive Web Graphics via “Plotly.js”. https://CRAN.R-project.org/package=plotly.