How to Change Plot Options in R
To convey a more powerful and impactful message to the viewer, you can change the look and feel of plots in R using R’s numerous plot options.
You can find all the documentation for changing the look and feel of base graphics in the Help page ?par(). This function allows you to set (or query) the graphical parameters or options.
Notice that par() takes an extensive list of arguments. Here are a few of the most commonly used options.
How to change the axes label style in R
To change the axes label style, use the graphics option las (label style). This changes the orientation angle of the labels:
0: The default, parallel to the axis
1: Always horizontal
2: Perpendicular to the axis
3: Always vertical
For example, to change the axis style to have all the axes text horizontal, use las=1 as an argument to plot:
> plot(faithful, las=1)
How to change the box type on an R plot
To change the type of box round the plot area, use the option bty (box type):
“o”: The default value draws a complete rectangle around the plot.
“n”: Draws nothing around the plot.
“l”, “7”, “c”, “u”, or “]”: Draws a shape around the plot area that resembles the uppercase letter of the option. So, the option bty=”l” draws a line to the left and bottom of the plot.
To make a plot with no box around the plot area, use bty=”n” as an argument to plot:
> plot(faithful, bty="n")
How to change more than one plot option in R
To change more than one graphics option in a single plot, simply add an additional argument for each plot option you want to set. For example, to change the label style, the box type, the color, and the plot character, try the following:
> plot(faithful, las=1, bty="l", col="red", pch=19)
How to change font size of text and axes on R plots
To change the font size of text elements, use cex (short for character expansion ratio). The default value is 1. To reduce the text size, use a cex value of less than 1; to increase the text size, use a cex value greater than 1.
> x <- seq(0.5, 1.5, 0.25) > y <- rep(1, length(x)) > plot(x, y, main="Effect of cex on text size") > text(x, y+0.1, labels=x, cex=x)
To change the size of other plot parameters, use the following:
cex.main: Size of main title
cex.lab: Size of axis labels (the text describing the axis)
cex.axis: Size of axis text (the values that indicate the axis tick labels)
> plot(x, y, main="Effect of cex.main, cex.lab and cex.axis", + cex.main=1.25, cex.lab=1.5, cex.axis=0.75)