Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot geom_text font size control

Tags:

r

ggplot2

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()) ?

like image 311
olala Avatar asked Jul 31 '14 14:07

olala


People also ask

How do I change the size of text in Geomation?

The default font size of geom_text() is 3.88. You can change the size using the size argument in geom_text() for a single plot. If you want to use the same updated size, you can set this with update_geom_defaults() , e.g. update_geom_defaults("text", list(size = 6)) .

How do I change the size of my Ggtitle?

To change the size of the title and subtitle, we add the theme() function to labs() or ggtitle() function, whatever you used. Here we use labs() function. Inside theme() function, we use plot. title parameter for doing changes in the title of plot and plot.

What is ggplot2 default font?

R and ggplot can create fantastic graphs, but the default Arial/Helvetica font is too boring and standard. You can change the font used in a plot fairly easily three different ways: All of the built-in ggplot themes have a base_family argument for setting the overall font family for the plot.

How do I change the axis title size in ggplot2?

You can change axis text and label size with arguments axis. text= and axis. title= in function theme() . If you need, for example, change only x axis title size, then use axis.


1 Answers

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)

like image 50
user20650 Avatar answered Sep 29 '22 21:09

user20650