r - ggplot2 道奇重叠 - 保留每个元素的宽度

标签 r ggplot2

希望它会很容易理解。与 here 的例子基本相同.

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

最佳答案

该参数已添加到 position_dodge在开发版in january .它还没有在 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 的数据中。

关于r - ggplot2 道奇重叠 - 保留每个元素的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728723/

相关文章:

r - 如何仅选择r中数据帧每年出现的个体

r - 将参数传递给 react 函数

r - plotly + ggplot2 : Some tooltips missing in stacked barplot

r - 在颜色空间中的scale_fill/color中粘贴名称不能循环工作

r - 以编程方式选择 data.table 中的列名

r - 是否可以仅使用响应变量拟合线性模型?

r - 使用 R httr GET 函数的 `query` 参数发送向量

R - 通过调节不同大小的序列来加速嵌套循环(矢量化?)

r - ggplot2 中的 cdplot() 模拟

r - 如何将我的图例组织成子组?