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())
?
Best Answer
Here are a few options for changing text / label sizes
The
size
in thegeom_text
changes the size of thegeom_text
labels.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 totheme
sizes.So a horrible fix for uniform sizes is to scale by this ratio
This of course doesn't explain why? and is a pita (and i assume there is a more sensible way to do this)