152

I tried to change the font to 10 for the labels of my bar plot in ggplot2 by doing something like this:

ggplot(data=file,aes(x=V1,y=V3,fill=V2)) +
    geom_bar(stat="identity",position="dodge",colour="white") + 
    geom_text(aes(label=V2),position=position_dodge(width=0.9),
                                                 hjust=1.5,colour="white") +
    theme_bw()+theme(element_text(size=10))

ggsave(filename="barplot.pdf",width=4,height=4)

but the resulting image has super big font size for the bar plot labels.

Then I thought of modifying in geom_text() with this:

geom_text(size=10,aes(label=V2),position=position_dodge(width=0.9),
                                                   hjust=1.5,colour="white")

The label font is even bigger...

I can change the size within geom_text to something like 3 and now it looks like font 10, similar to the axis labels.

I'm wondering what's going on? Does theme(text=element_text(size=10)) doesn't apply to labels?

And why size of 10 in geom_text() is different from that in theme(text=element_text()) ?

4 Answers 4

233

Here are a few options for changing text / label sizes

library(ggplot2)

# Example data using mtcars

a <- aggregate(mpg ~ vs + am , mtcars, function(i) round(mean(i)))

p <- ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
            geom_bar(stat="identity",position="dodge") + 
            geom_text(data = a, aes(label = mpg), 
                            position = position_dodge(width=0.9),  size=20)

The size in the geom_text changes the size of the geom_text labels.

p <- p + theme(axis.text = element_text(size = 15)) # changes axis labels

p <- p + theme(axis.title = element_text(size = 25)) # change axis titles

p <- p + theme(text = element_text(size = 10)) # this will change all text size 
                                                             # (except geom_text)


For this And why size of 10 in geom_text() is different from that in theme(text=element_text()) ?

Yes, they are different. I did a quick manual check and they appear to be in the ratio of ~ (14/5) for geom_text sizes to theme sizes.

So a horrible fix for uniform sizes is to scale by this ratio

geom.text.size = 7
theme.size = (14/5) * geom.text.size

ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
  geom_bar(stat="identity",position="dodge") + 
  geom_text(data = a, aes(label = mpg), 
            position = position_dodge(width=0.9),  size=geom.text.size) + 
  theme(axis.text = element_text(size = theme.size, colour="black")) 

This of course doesn't explain why? and is a pita (and i assume there is a more sensible way to do this)

Sign up to request clarification or add additional context in comments.

4 Comments

interesting, what did you check to find out the 14/5 ratio ?
i see. you remind me of something I read recently, I guess it's the difference in units, geom_text default of 5 might be 5mm and the theme() size unit is point. 1 point is 1/72 inch=0.35mm, so 1 in geom_text() is 1mm, 1/0.35 =~ 14/5 :)
And for those of us who want to go the other way, and convert geom_text to "regular" font size, you can just multiply by 0.36.
25

I had this question, and found on the aesthetic specifications ggplot help page that they add in a handy function for converting mm (default for geom_text for consistency with lines and point) to points (the scale for theme)- you simply put in your font size (in pt), then type /.pt.

So your code would be:

ggplot(data=file,aes(x=V1,y=V3,fill=V2)) +
geom_bar(stat="identity",position="dodge",colour="white") + 
geom_text(aes(label=V2),position=position_dodge(width=0.9),
          hjust=1.5,colour="white", size = 10/.pt) +
theme_bw()

This conversion does the equivalent of what Nova mentioned above. Hope this is helpful for anyone looking for this answer in the future.

1 Comment

This should be the accepted answer as of 2023. Use the built-in helper function .pt to divide your pt-input by.
3

Take a look at the relevant entry in ggplot2's customization FAQ: https://ggplot2.tidyverse.org/articles/faq-customising.html#what-is-the-default-size-of-geom_text-and-how-can-i-change-the-font-size-of-geom_text

You can modify the default size of geom_text() by placing update_geom_defaults("text", list(size = X), where X is your choice of new size, at the beginning of your script.

Comments

0

Adding to the accepted answer:

Look up ?.pt and you will understand what 14/5 means and why it is needed.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.