Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

How to change font size of text and axes on R plots

Asked by Minta Ankney May 22, 2024 37.2K views 4 answers
Share

About this question

not available

Your answer

4 Answers

Kalenstroud Latest answer

Answered on Sep 14, 2026

Yes, cex is a useful way to control text size in base R plots. You can also adjust specific elements separately, such as cex.main for the title, cex.lab for axis labels, cex.axis for axis tick labels, and cex.sub for subtitles. For more control over how text looks, https://typetype.org/ is also worth exploring when choosing fonts for data visualizations.

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Jan 20, 2025

How to Change Font Size of Text and Axes on R Plots

R provides multiple ways to customize the font size of text and axes in plots, enabling users to improve plot readability and presentation. Here’s how you can adjust these settings:

1. Using cex Parameter

The cex parameter controls the size of text elements relative to the default size.

  • cex.main: Adjusts the title size.
  • cex.lab: Adjusts the axis label size.
  • cex.axis: Adjusts the tick mark label size.
  • cex.sub: Adjusts the subtitle size.

Example:

plot(1:10, main = "Main Title", xlab = "X-axis", ylab = "Y-axis",
     cex.main = 1.5, cex.lab = 1.2, cex.axis = 1, cex.sub = 0.8)

2. Using par() Function

The par() function can set graphical parameters globally before creating a plot.

par(cex.main = 1.5, cex.lab = 1.2, cex.axis = 1)
plot(1:10, main = "Main Title", xlab = "X-axis", ylab = "Y-axis")

3. Customizing Font Size with ggplot2

For ggplot2 users, font sizes can be customized using the theme() function.

Example:

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Title", x = "X-axis", y = "Y-axis") +
  theme(plot.title = element_text(size = 16),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12))

4. Using text() for Custom Text

To add custom text elements to a plot, use text() with the cex parameter.

  plot(1:10)text(5, 8, "Custom Text", cex = 2)

Key Takeaways

  • Use cex for base R plots and theme() for ggplot2 to adjust font sizes.
  • par() can set global font size preferences in base R.

Customize sizes for individual plot elements (title, axis labels, tick marks) for precise control.


Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 18, 2024

To adjust the font size of text and axes on R plots, you can use various functions and parameters within the base R graphics system or through packages like ggplot2. Here's how you can do it using both methods:

Base R Graphics:

You can use the par() function to set graphical parameters, including font sizes. Here's an example:

# Set up example data
x <- 1:10
y <- 1:10
# Create a plotplot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")# Adjust font size of title, x-axis label, and y-axis labelpar(cex.main = 2, cex.lab = 1.5)

In this example, cex.main controls the size of the main title, and cex.lab controls the size of the axis labels.

ggplot2:

With ggplot2, you can adjust font sizes using the theme() function. Here's an example:

  library(ggplot2)# Set up example datadf &lt;- data.frame(x = 1:10, y = 1:10)# Create a ggplot objectp &lt;- ggplot(df, aes(x, y)) +  geom_point() +  labs(title = "Scatter Plot", x = "X-axis", y = "Y-axis")# Adjust font size of title, x-axis label, and y-axis labelp + theme(  plot.title = element_text(size = 16),  axis.title.x = element_text(size = 14),  axis.title.y = element_text(size = 14))

In this example, size within element_text() controls the font size of the plot title and axis labels.

Feel free to adjust the font size values (cex.main, cex.lab, size) according to your preferences. These are just examples to demonstrate how to change font sizes on R plots using both base R graphics and ggplot2.


Was this helpful?

More Data Science discussions