2016-03-14 83 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

相關問題