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
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.
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.
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.
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.
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?