How can the legend size be changed in a base R plot? 2

How can the legend size be changed in a base R plot?

The legend size in a base R plot can be changed by adjusting the “cex” parameter, which stands for “character expansion”. This parameter controls the size of the text and symbols in the legend. By increasing or decreasing the value of “cex”, the size of the legend can be adjusted accordingly. This can be done by specifying the “cex” value within the “legend” function or by using the “par” function to set it globally for all legends in the plot. Additionally, the “pt.cex” parameter can be used to specifically adjust the size of the legend symbols. Overall, adjusting these parameters allows for easy customization of the legend size in a base R plot.

Change Legend Size in Base R Plot (With Examples)


The easiest way to change the size of a legend in a base R plot is to use the cex argument:

legend('topright', legend=c('A', 'B'), col=1:2, pch=16, cex=1)

The default value for cex is 1.

The larger the value that you specify for cex, the larger the legend will be.

The following example shows how to use this argument in practice.

Example: Change Legend Size in Base R Plot

Suppose we create the following scatter plot in base R:

#create data frame
df <- data.frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(4, 6, 7, 12, 6, 8),
                 group=c(1, 1, 1, 2, 2, 2))

#create scatter plot
plot(df$x, df$y, col=df$group, pch=16)

#add legend in top right corner
legend('topright', legend=c('First', 'Second'),
       col=1:2, pch=16)

To increase the legend size, we can increase the value for cex to some value greater than 1:

#create scatter plot
plot(df$x, df$y, col=df$group, pch=16)

#add legend in top right corner with increased size
legend('topright', legend=c('First', 'Second'),
       col=1:2, pch=16, cex=2)

increase legend size in base R plot

Notice how much larger the legend is in this plot compared to the previous plot.

To decrease the legend size, we can decrease the value for cex to some value less than 1:

#create scatter plot
plot(df$x, df$y, col=df$group, pch=16)

#add legend in top right corner with decreased size
legend('topright', legend=c('First', 'Second'),
       col=1:2, pch=16, cex=.75)

decrease legend size in base R plot

Also note that you can change the point size in a legend by changing the value for the pt.cex argument.

#create scatter plot
plot(df$x, df$y, col=df$group, pch=16)

#add legend in top right corner with increased point size
legend('topright', legend=c('First', 'Second'),
       col=1:2, pch=16, pt.cex=2)

Notice that the legend size is the same, but the red and black points in the legend are twice as large.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

Cite this article

stats writer (2024). How can the legend size be changed in a base R plot?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-legend-size-be-changed-in-a-base-r-plot/

stats writer. "How can the legend size be changed in a base R plot?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-legend-size-be-changed-in-a-base-r-plot/.

stats writer. "How can the legend size be changed in a base R plot?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-legend-size-be-changed-in-a-base-r-plot/.

stats writer (2024) 'How can the legend size be changed in a base R plot?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-legend-size-be-changed-in-a-base-r-plot/.

[1] stats writer, "How can the legend size be changed in a base R plot?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the legend size be changed in a base R plot?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top