2017-08-17 61 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数据。