Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the font size of a ggplot chart

Tags:

r

ggplot2

In a ggplot chart I have a title for chart and values for x and y axis. When I save the chart as an image the labels are tiny.

I tried to change the font size using:

size = 21

But it didn't work. Is there any easy way to chage the font size for the whole chart?

like image 994
Sara Balloiuk Avatar asked Aug 28 '13 11:08

Sara Balloiuk


1 Answers

There are two ways to set the global font size in a ggplot object p = ggplot(data, aes(x=x, y=y)). As Didzis pointed out one can specify:

global_size = 10
p + theme(text = element_text(size=global_size))

Alternatively, if you are using one of the ggplot themes, pass base_size argument:

p +  theme_classic(base_size = global_size)

This is subjective, but I would advise against using font size 21. It is better to use "standard" font sizes (9-12 pt) and adjust the height and width when saving the ggplot object to match your final printed output.

Just measure the width / height in inches (or cm) of the figure in your presentation, report, etc and save the plot p with exactly those dimensions

ggsave(p, filename=filename,
 width=width_measured, height=height_measured, units='cm')
like image 61
Vlad Kim Avatar answered Sep 23 '22 09:09

Vlad Kim