2017-08-28 60 views
1

我想使用ggplot2中的geom_count圖形,但數值範圍太小,圖例符號變成浮點數,例如發生計數。 1 1.5 2 2.5 3ggplot2 geom_count設置圖例符號整數

下面是測試情況:

test = mtcars[1:6,] 

ggplot(test, aes(cyl, carb)) + 
    geom_count(aes(color = ..n.., size = ..n..)) + 
    guides(color = 'legend') 

如何讓只有在全整數發生斷裂?

回答

3

您可以設置連續colorsize比例的breaks比例。

可以給用於在間斷值的矢量,但每個文檔中的breaks參數也可以被下式給出:

,是以限制作爲輸入並返回場所作爲輸出的函數

因此,對於像您的示例這樣的簡單案例,您可以使用as.integerround作爲函數。

ggplot(test, aes(cyl, carb)) + 
    geom_count(aes(color = ..n.., size = ..n..)) + 
    guides(color = 'legend') + 
    scale_color_continuous(breaks = round) + 
    scale_size_continuous(breaks = round) 

對於更大範圍的比你的例子整數,你可以手動輸入中斷,例如,breaks = 1:3,或者寫一個函數,規模的限制,並返回一個整數序列。您可以使用此功能breaks

這可能是這樣的:

set_breaks = function(limits) { 
    seq(limits[1], limits[2], by = 1) 
} 

ggplot(test, aes(cyl, carb)) + 
    geom_count(aes(color = ..n.., size = ..n..)) + 
    guides(color = 'legend') + 
    scale_color_continuous(breaks = set_breaks) + 
    scale_size_continuous(breaks = set_breaks) 
+0

使用矢量工作的感謝,但是當我用'round'我失去了價值?這裏有一個測試例test = data.frame(cyl = c(rep(4,3),rep(2,2),1),carb = c(rep(6,3),rep(2,2) ,5))'在哪裏使用中斷=四捨五入結果在1和3而不是1,2,3破壞 – voiDnyx

+0

@voiDnyx請參閱編輯 – aosmith