geom_text中每个标签的字体大小不同

时间:2017-09-15 07:13:16

标签: r ggplot2

我有一个使用ggplot2制作的条形图。我想使用geom_text为每个栏添加标签,以使标签的文字大小与标签相对应。为此,我使用了以下代码:

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, size = mpg), 
                position = position_dodge(width=0.9))

这给了我一个看起来像这样的情节: enter image description here

如您所见,标签尺寸正在变化,但文字字体大小与标签尺寸不符。对于第一个标签,标签是15,几乎看不到。当我绘制固定文字大小为15的相同条形图时,标签不像上面所见的那么小。以下是使用固定文本大小生成的代码和图:

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

enter image description here

当为每个标签指定不同尺寸时,是否有办法使标签尺寸保持一致?

2 个答案:

答案 0 :(得分:3)

将大小设置为[ { "allquestions": [ { "question": "First question in 0th index" }, { "question": "Second question in 0th index" } ] }, { "allquestions": [ { "question": "First question in 1st index" } ] } ] 可以解决问题

NSPredicate *combinePagePredicate = [NSPredicate predicateWithFormat:@"allquestions == %@",@"someValue"];

snap1

答案 1 :(得分:1)

您可以更改scale_size

的范围

例如,如果您希望缩放的大小范围从字体大小15到字体大小28:

ggplot(mtcars, aes(factor(vs), y=mpg, fill=factor(am))) + 
  geom_bar(stat="identity", position="dodge") + 
  geom_text(data = a, aes(label = mpg, size = mpg), 
            position = position_dodge(width=0.9)) +
  scale_size(range = c(15, 28), guide = F) #legend hidden

enter image description here

相关问题