More colour options are available with other packages (see the excellent RColorBrewer package) or you can even ‘mix’ your own colours using the colorRamp() function (see ?colorRamp for more details). To see a list of all 657 preset colours available in base R use the colours() function (you can also use colors()) or perhaps even easier see this link. For example, col = "red" changes the plotting symbol to red. This argument can either take an integer value to specify the colour or a character string giving the colour name.
The col = argument changes the colour of the plotting symbols. Here’s a summary table displaying the value and corresponding symbol type. Symbols 0 to 14 are open symbols, 15 to 20 are filled symbols and 21 to 25 are symbols where you can specify a different fill colour and outside line colour. The pch = argument takes an integer value between 0 and 25 to define the type of plotting symbol. We can also change the type of plotting symbol, the colour of the symbol and the size of the symbol using the pch =, col = and cex = arguments respectively. This value needs to be negative as we want the tick marks to be outside the plotting region (see what happens if you set it to tcl = 0.2). We can also make the tick marks just a little shorter by setting tcl = -0.2. We’ll choose 0.8 making our text 20% smaller. The cex.axis = argument requires a number giving the amount by which the text will be magnified (or shrunk) relative to the default value of 1. While we’re about it let’s also rotate the y axis tick mark labels so they read horizontally using by setting the las = 1 argument in the plot() function and make them a tad smaller with the cex.axis = argument. We can change this by setting the xaxs = "i" and yaxs = "i" arguments when we use the par() function. One of the things that we still don’t like is that by default the x and y axes do not intersect at the origin (0, 0) and both axes extend beyond the maximum value of the scale by a little bit.
OK, that’s looking a lot better already after only a few adjustments.
Lastly, this section covers the basics of how to customise base R graphics and most (if not all) of these approaches will not work for plots created with the lattice graphics system.
This requires a little experience (and trial and error), but again becomes easier with practice. We often start with a basic layout of our plot and then add layers of complexity until we achieve the desired results.
Thirdly, learning how to customise plots in base R isn’t just about what code you need to use, it’s also about learning the process of building a plot. Often a quick Google or peek at the relevant help pages will jog your memory. This isn’t necessarily a bad thing as this is what makes base R graphics so flexible but it’s a lot to take in. Secondly, when you start customising plots you’re confronted with a huge number of options and arguments to try and remember. If you crave a little more consistency take a look at Chapter 5 where we introduce the excellent ggplot2 package. This can be a little frustrating to begin with but gets easier the more experience you gain. What works with the plot() function isn’t guaranteed to necessarily work with the boxplot() function. Firstly, although many of the approaches we introduce in this section will work with most base R plotting functions, there’s no true consistency between functions. There are however a couple of things to bear in mind. The good news is that the base R graphics system allows you to change almost any aspect of your plot. If however, you’d like to make them a little prettier (for your thesis, publication or even your own amusement) you’ll need to invest some time learning how to customise your plots. 1.4.2 Integrated developement environementsĪll of the plots we’ve created so far in this Chapter are more than suitable for exploring your data.