In data analysis and visualization, the presentation of the data can be just as important as the data itself. A clear and well-structured graph can significantly impact the interpretation and understanding of the underlying data. One such aspect that contributes to the quality of data visualization is the font size used in plots. This article will explore how to manipulate the font size in base R plots.

## Introduction to Base R Plotting

R is equipped with a base plotting system that allows users to generate various types of plots such as scatter plots, line plots, histograms, and more. The functions such as `plot()`

, `hist()`

, `boxplot()`

, etc., are part of this base plotting system.

The functions like `text()`

, `mtext()`

, `title()`

, and `axis()`

allow adding text, main titles, subtitles, axis labels, etc., to the plot. Each of these functions has an argument called `cex`

, `cex.main`

, `cex.sub`

, `cex.lab`

, or `cex.axis`

, which controls the font size in different elements of the plot.

## Understanding the Font Size Parameters in R

Before we dive into examples, let’s familiarize ourselves with the parameters that control font size in base R plots:

`cex`

: This parameter is a numerical value that sets the amount by which plotting text and symbols should be scaled relative to the default. A`cex`

value of 1 means no scaling, less than 1 means reduction, and greater than 1 means an increase in size.`cex.main`

: The magnification to be used for main titles relative to the current setting of`cex`

.`cex.sub`

: The magnification to be used for subtitles relative to the current setting of`cex`

.`cex.lab`

: The magnification to be used for x and y labels relative to the current setting of`cex`

.`cex.axis`

: The magnification to be used for axis annotation relative to the current setting of`cex`

.

## Changing Font Size in R Base Plots

Let’s use R’s built-in ‘mtcars’ dataset to illustrate how to change font sizes in base R plots. We will create a simple scatter plot of car weights (wt) against miles per gallon (mpg):

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
xlab = "Car Weights",
ylab = "Miles per Gallon")
```

### Scaling Text and Symbols: cex

The `cex`

argument controls the size of the points in the plot. A value of 1 plots the points at the default size, while values less than 1 reduce the point size and values greater than 1 increase the point size.

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
xlab = "Car Weights",
ylab = "Miles per Gallon",
cex = 1.5)
```

This code generates the same scatter plot but with larger points due to the `cex = 1.5`

argument.

### Adjusting Main Title Size: cex.main

The `cex.main`

argument controls the size of the main title of the plot:

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
xlab = "Car Weights",
ylab = "Miles per Gallon",
cex.main = 2)
```

In this example, `cex.main = 2`

doubles the size of the main title compared to the default.

### Adjusting Subtitle Size: cex.sub

The `cex.sub`

argument controls the size of the subtitle of the plot:

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
sub = "Source: Motor Trend US Magazine",
xlab = "Car Weights",
ylab = "Miles per Gallon",
cex.sub = 0.8)
```

Here, `cex.sub = 0.8`

reduces the size of the subtitle to 80% of the default size.

### Adjusting Axis Label Size: cex.lab

The `cex.lab`

argument controls the size of the x and y axis labels:

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
xlab = "Car Weights",
ylab = "Miles per Gallon",
cex.lab = 1.2)
```

In this case, `cex.lab = 1.2`

increases the size of the x and y axis labels by 20% compared to the default size.

### Adjusting Axis Annotation Size: cex.axis

The `cex.axis`

argument controls the size of the axis annotation:

```
plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of Car Weights vs MPG",
xlab = "Car Weights",
ylab = "Miles per Gallon",
cex.axis = 0.75)
```

Here, `cex.axis = 0.75`

reduces the size of the axis annotation to 75% of the default size.

## Conclusion

Understanding how to manipulate font size in R plots is essential for effective data visualization. By adjusting font sizes, you can emphasize key points, improve readability, and make your plots more aesthetically pleasing.

While this article has focused on base R plotting, it’s worth noting that similar concepts apply when using other plotting systems in R like ggplot2 and lattice. So, these skills are widely applicable and will serve you well in your data visualization journey in R.