R: How to Change Font Size in corrplot


A correlation matrix is a matrix that shows the correlation coefficients between each pairwise combination of variables in a dataset.

This is a particularly useful data visualization to create because it allows you to quickly understand the relationship between various pairwise combinations of variables in a dataset and it allows you to see which variables have a significant linear association.

One of the easiest ways to create a correlation matrix in R is by using the corrplot function from the corrplot package.

This function uses the following basic syntax:

library(corrplot)

#create correlation matrix with correlation coefficients shown inside matrix
corrplot(cor(df), method='number')

This function can produce a correlation matrix in which the correlation coefficient between each combination of variables is shown inside each cell of the matrix.

Often you may want to adjust the font size of the correlation coefficients shown in the matrix, which you can do by using the number.cex argument as follows:

library(corrplot)

#create correlation matrix increased font size for correlation coefficients
corrplot(cor(df), method='number', number.cex=1.5)

The larger the value that you use for number.cex, the larger the font size of the correlation coefficients will be inside the matrix.

You can adjust this value depending on how small or larger you would like the font to be.

The following example shows how to use the number.cex argument in practice.

Example: How to Change Font Size in corrplot in R

For this example, we will create the following data frame that contains information about various basketball players:

#create data frame
df <- data.frame(assists=c(4, 5, 5, 6, 7, 8, 8, 10),
                 rebounds=c(12, 14, 13, 7, 8, 8, 9, 13),
                 points=c(22, 24, 26, 26, 29, 32, 20, 14),
                 steals=c(5, 6, 7, 7, 8, 5, 3, 4))

#view data frame
df

  assists rebounds points steals
1       4       12     22      5
2       5       14     24      6
3       5       13     26      7
4       6        7     26      7
5       7        8     29      8
6       8        8     32      5
7       8        9     20      3
8      10       13     14      4

This data frame contains the following information about various players:

  • assists: The total assists made by a player
  • rebounds: The total rebounds collected by a player
  • points: The total points scored by a player
  • steals: The total steals made by a player

Suppose that we would like to create a correlation matrix to view the linear relationship between each pairwise combination of these four variables in the data frame.

We can use the following basic syntax with the corrplot() function from the corrplot package to do so:

library(corrplot)

#create correlation matrix
corrplot(cor(df), method='number')

This produces a correlation matrix in which the correlation coefficient for each pairwise combination of variables in the data frame is shown inside each cell of the matrix:

R corrplot example

Suppose that we would like to increase the font size of the correlation coefficients shown inside the matrix.

We can use the following syntax with the number.cex argument to do so:

library(corrplot)

#create correlation matrix with increased font size
corrplot(cor(df), method='number', number.cex=1.5)

This produces the following result:

R corrplot font size

Notice that the font size of the correlation coefficients is much larger in this correlation matrix.

Feel free to use whatever value you would like for the number.cex argument to make the font size as large as you would like.

Additional Resources

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

How to Calculate Correlation in R with Missing Values
How to Calculate Spearman Rank Correlation in R
How to Calculate Cross Correlation in R
How to Calculate Rolling Correlation in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *