ggplot geom_text字体大小控件 [英] ggplot geom_text font size control
问题描述
我试图通过做这样的事情来将字体的字体更改为10: ggplot2
中的条形图标签
ggplot(data = file,aes(x = V1,y = V3,fill = V2))+
geom_bar(stat =identity,position = (aes(label = V2),position = position_dodge(width = 0.9),
hjust = 1.5,color =white)+
theme_bw()+ theme(element_text(size = 10))
ggsave(filename =barplot.pdf,width = 4,height = 4)
但是结果图像的条形图标签的字体超大。
然后
geom_text() size = 10,aes(label = V2),position = position_dodge(width = 0.9),
hjust = 1.5,color =white)
标签字体甚至更大......
我可以用i更改尺寸n geom_text
类似于3,现在它看起来像字体10,类似于轴标签。
我想知道发生了什么事?是否主题(text = element_text(size = 10))
不适用于标签?
为什么在 geom_text()
中的大小不等于主题中的大小( text = element_text())
?
library(ggplot2)
#使用mtcars的示例数据
a
p < - ggplot(mtcars,aes(因子(vs),y = mpg, fill = factor(am)))+
geom_bar(stat =identity,position =dodge)+
geom_text(data = a,aes(label = mpg),
position = $ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' / code>在 geom_text
中更改 geom_text
标签的大小。
$ b $ pre $ p < - p +主题(axis.text = element_text(size = 15))#改变轴标签
p < - p + theme(text = element_text(size = 10)b
p <-p + theme(axis.title = element_text(size = 25))#change axis titles
p < )#这将改变所有文本大小
#(geom_text除外)
对于这个为什么geom_text()中的大小10与theme(text = element_text())中的大小不同?
是的,它们是不同的。我做了一个快速手动检查,他们似乎在〜(14/5)的比例为 所以统一尺码的一个可怕的修正是按比例缩放 这当然不能解释为什么?并且是皮塔饼(我认为还有更多明智的方式来做到这一点) I tried to change the font to 10 for the labels of my bar plot in but the resulting image has super big font size for the bar plot labels. Then I thought of modifying in The label font is even bigger... I can change the size within I'm wondering what's going on? Does And why size of 10 in Here are a few options for changing text / label sizes The 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 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) 这篇关于ggplot geom_text字体大小控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! geom_text
尺寸为主题$ c $大小。
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,color =black))
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)
geom_text()
with this:geom_text(size=10,aes(label=V2),position=position_dodge(width=0.9),
hjust=1.5,colour="white")
geom_text
to something like 3 and now it looks like font 10, similar to the axis labels. theme(text=element_text(size=10))
doesn't apply to labels? geom_text()
is different from that in theme(text=element_text())
?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)
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)
geom_text
sizes to theme
sizes. 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"))