ggplot2避免重叠 - 保留每个元素的宽度

6
希望这个例子容易理解。它基本上与这里的示例相同。

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")

为了避免出现像下面这样的超宽条,而是与那里相同。 在此输入图片描述

我也遇到了这个错误。希望能得到一个合适的解决方案。 - Thirst for Knowledge
@ThirstforKnowledge 目前还没有发现什么东西。我正在考虑建立虚拟数据来填充空值,但它是为闪亮而设计的,所以我需要以某种方式自动化一切,并使函数读取确实缺少了什么。 - AlienDeg
1个回答

4

这个参数是在最新开发版的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)

结果图

这个方法有效的原因是在传递给geom的数据中包括了零计数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接