Sara Balloiuk
Sara Balloiuk

Reputation: 279

Change the font size of a ggplot chart

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?

Upvotes: 16

Views: 35601

Answers (2)

Vlad Kim
Vlad Kim

Reputation: 181

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')

Upvotes: 18

Didzis Elferts
Didzis Elferts

Reputation: 98579

Size of texts are changed using function theme() and then choosing element you need to modify. To set font size for all texts in plots attribute text should be changed.

ggplot(mtcars,aes(cyl,mpg))+geom_point()+theme(text=element_text(size=21))

Upvotes: 23

Related Questions