You can use the cex argument within the plot() function in R to change the size of symbols and text relative to the default size.
The default value for cex is 1.
A value of 2 will double the size and a value of 0.5 will cut the size in half.
There are actually five cex arguments you can use to change the size of specific plot elements:
- cex: Changes the size of symbols
- cex.axis: Changes the size of axis tick mark annotations
- cex.lab: Changes the size of x-axis and y-axis labels
- cex.main: Changes the size of the plot title
- cex.sub: Changes the size of the plot subtitle
The following example shows how to use these arguments in practice.
Example: Use cex to Change Size of Plot Symbols
Suppose we have the following data frame in R:
#create data frame df <- data.frame(x=c(1, 2, 2, 4, 5, 3, 5, 8, 12, 10), y=c(5, 9, 12, 14, 14, 13, 10, 6, 15, 18)) #view data frame df x y 1 1 5 2 2 9 3 2 12 4 4 14 5 5 14 6 3 13 7 5 10 8 8 6 9 12 15 10 10 18
Suppose we use the plot() function in R to create a simple scatterplot:
#create scatterplot of x vs. y plot(df$x, df$y, pch=19, main='Scatterplot of x vs. y')
Note: The argument pch=19 specifies that a filled-in circle should be used as the symbol for the points in the plot.
By default, the symbols in the plot and the text elements all have a cex value of 1.
However, we can use the various cex arguments to change the sizes of the symbols and the text elements in the plot relative to the default size:
#create scatterplot with custom symbol and text sizes plot(df$x, df$y, pch=19, main='Scatterplot of x vs. y', cex=2, cex.main=3, cex.lab=1.5, cex.axis=2)
Notice that the size of the symbols and the text elements have all changed.
Here’s exactly how we changed the various plot elements:
- cex=2: Increased the size of the circles in the plot 2 times.
- cex.main=3: Increased the size of the title text by 3 times.
- cex.lab=1.5: Increased the size of the x and y-axis labels by 1.5 times.
- cex.axis=2: Increased the size of the tick mark annotations by 2 times.
Feel free to play around with the values for each of these cex arguments to create a plot with the exact sizes that you’d like.
The following tutorials explain how to perform other common tasks in R:
How to Use par() to Create Multiple Plots in R
How to Change Legend Position in R Plots
How to Change Font Size in R Plots