ggplot geom_text字体大小控件 [英] ggplot geom_text font size control

查看:1925
本文介绍了ggplot geom_text字体大小控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过做这样的事情来将字体的字体更改为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)的比例为 geom_text 尺寸为主题

所以统一尺码的一个可怕的修正是按比例缩放

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

这当然不能解释为什么?并且是皮塔饼(我认为还有更多明智的方式来做到这一点)


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

解决方案

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)

这篇关于ggplot geom_text字体大小控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆