Data Science

What is the correct syntax to modify font sizes for labels and titles in R plots and RStudio?

AM Asked by Amanda Roberts · 14-03-2024
0 upvotes 12,716 views 0 comments
The question

I am currently working on a data science project in R and I’m struggling to make my plot labels readable. When I export my graphs, the text is far too small. I need to know how to increase the font size for the main title, axis labels, and legend. Does the method differ if I am using base R functions versus the ggplot2 package? Also, is there a global setting to increase the font size of the RStudio console and editor for better accessibility while I’m coding?

3 answers

0
KI
Answered on 15-03-2024

For base R plots, you use the cex (character expansion) parameter. For example, plot(x, y, main="Title", cex.main=2, cex.lab=1.5) will double the title size and increase axis labels by 50%. However, if you are using ggplot2, which is the industry standard for data science, you must use the theme() function. Specifically, you would add + theme(text = element_text(size = 16)) to change all text globally, or axis.title = element_text(size = 14) for specific elements.

If you simply want to make the RStudio interface larger, you can go to Tools > Global Options > Appearance and adjust the "Editor font size." This won't affect your plots, but it makes the development environment much easier on the eyes.

0
JE
Answered on 17-03-2024

Are you finding that the font size looks different in the RStudio plotting pane compared to when you save the file using png() or pdf()? I ask because the resolution (DPI) and the dimensions you specify during the export process often override the font size settings you see on your screen, leading to blurry or tiny text in your final reports.

TH 18-03-2024

Jeffrey, I am definitely having that exact issue! My plots look great in the preview, but they are unreadable in my PDF report. Should I be using the res parameter in the png() function, or is it better to use the ggsave() function with specific height and width arguments to ensure the font scaling remains consistent across different output formats?

0
SA
Answered on 20-03-2024

For ggplot2, I always recommend using rel(). Setting size = rel(1.5) makes the text 1.5 times the size of the base theme, which helps maintain proportions.

AM 21-03-2024

Great point, Sarah! Using rel() is much safer than hardcoding pixel values because it ensures that if you change the overall base size later, your headers and labels scale proportionally. I started using this in my Machine Learning reports and it saved me a lot of re-formatting time.

Share your thoughts

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

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session