An Introduction to R and RStudio

Basic Plotting/Graphing in R

This last section of the workshop introduces the concepts required to start you on your way to producing plots in R. R is renowned for its ability to output publication quality graphs, check out this repository if you’d like an example of the wide array of possible graphs: http://rgm.ogalab.net/RGM

As you will notice in the above gallery (and the multitude of others available), the range of plots go from simple to ridiculously complex – or one line of code to several pages.fn29 As you’ll see most of the time starting simple - using plot(mydata) - will give you an initial starting point. Then it’s only a matter of ‘beautifying’ your graph using the lower-level functions and par().

In this section we are going to run through a variety of example plots, and then build upon them using par() and low-level functions. We’ll cover how to output plots to vary file types, including .pdf, .png, .jpeg, etc. This is demonstrated using code in R, but I will also show you how RStudio handles the output of your graphs. I’ve also included the R-colour chart2 at the end of this section. This chart is extremely useful when you want to use an array of different colours in your plot (or just want to be different).

Let’s get started.


fn29. If I haven’t made a note of it before, sometimes you’ll need to be careful about the use of color and colour. Most package designers take into account that not everybody is American and allow use of both spellings, but sometimes you’ll have to use one or the other. This is easy to check with R help.