2016-03-14 84 views
1

使用mtcars数据集,我编写了以下代码,该代码显示图表中文本标签的字体大小,取决于它们的'carb'计数。我想放大这些标签的相对字体大小,因为y轴上的最小值为3 - 太小。我发现了类似的帖子,但没有直接解决这个问题。更改ggplot 2中图表标签的相对字体大小R

ggplot(mtcars, aes(carb)) + 
    stat_count(aes(y = ..count..), 
     show.legend = F, geom = "text") + 
     element_text(aes(size = ..count.., label = ..count..)) 

回答

1

打印的数字的实际大小由scale_size_continuous控制。该比例采用参数range,该参数定义了用于最小和最大对象的尺寸。默认情况下,range = c(1,6)。你可以用这两个数字来玩,直到你得到所需的结果。

默认值:

ggplot(mtcars, aes(carb)) + 
    stat_count(aes(y = ..count..), 
      show.legend = F, geom = "text") + 
    element_text(aes(size = ..count.., label = ..count..)) + 
    scale_size_continuous(range = c(1, 6)) 

enter image description here

放大小的数字,但保持最大尺寸相同:

ggplot(mtcars, aes(carb)) + 
    stat_count(aes(y = ..count..), 
      show.legend = F, geom = "text") + 
    element_text(aes(size = ..count.., label = ..count..)) + 
    scale_size_continuous(range = c(3, 6)) 

enter image description here

放大所有数字:

ggplot(mtcars, aes(carb)) + 
    stat_count(aes(y = ..count..), 
      show.legend = F, geom = "text") + 
    element_text(aes(size = ..count.., label = ..count..)) + 
    scale_size_continuous(range = c(4, 12)) 

enter image description here

相关问题