2017-08-17 34 views
4

希望它會容易理解。它基本上與here一樣。ggplot2道奇重疊 - 保留每個元素的寬度

enter image description here

使用

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + 
    geom_bar(position = position_dodge(preserve = "single")) 

但是我卻越來越Error in position_dodge(preserve = "single") : unused argument (preserve = "single") /。 GGPLOT2版本2.2.1

那麼如何修改代碼

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + 
    geom_bar(position = "dodge") 

拿不到這個喜歡以下,但一樣有超寬吧。 enter image description here

+0

我也有這個錯誤。將不勝感激。 –

+0

@ThirstforKnowledge還沒有找到任何東西。實際上,我正在考慮構建虛擬數據來填充空值,但它是閃亮的,所以我需要以某種方式自動化所有內容,並使函數讀取缺少的內容。 – AlienDeg

回答

3

在開發版本in january中將該參數添加到position_dodge。它還沒有在CRAN上。

一種解決方法是計算外GGPLOT2統計:

ggplot(as.data.frame(with(mtcars, table(cyl = factor(cyl), vs = factor(vs)))), 
     aes(factor(cyl), y = Freq, fill = factor(vs))) + 
    geom_col(position = "dodge") + 
    scale_fill_discrete(drop = FALSE) 

resulting plot

這工作,因爲零計數包含在傳遞給GEOM數據。