r - 条形图 (ggplot2) 中的单个条形图错位

标签 r ggplot2 geom-bar

我正在制作一个包含 2 个面板的条形图,但似乎有一个条形图不在原位。它是右侧面板上部的栏。填写为TR,但它占据了TL的位置。

数据是:

DATA2=structure(list(A = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2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, 5L, 3L, 1L, 1L, 
4L, 1L, 1L, 1L, 5L, 1L, 4L), .Label = c("0-2", "2-4", "4-6", 
"6-8", "8-10"), class = c("ordered", "factor"))), row.names = c(NA, 
-240L), class = "data.frame")

剧情代码如下:

ggplot(DATA2, aes(x=C,fill=B)) +
  geom_bar(position = position_dodge(width = -0.9, preserve = "single")) +
  facet_grid(.~A) +
  theme_bw() + 
  scale_fill_manual(values = c("grey","grey40")) + 
  coord_flip()

enter image description here

尽管填充正确,但右侧面板上部的小条(计数 = 1)放错了位置。有小费吗?谢谢!

最佳答案

有些变量值的组合没有出现在您的数据中,即 c("Par","TL","8-10")c("Par”,“TR”,“2-4”)。当您使用 geom_bar(stat = "count")(这是默认设置)时,ggplot2 显然不会生成这些计数为 0 的组合。对于每个配对,条从顶部呈现向下。当其中一个计数不存在时,另一个将呈现在条形配对的顶部。

我们可以看到同样的事情是这个更简单的例子:

library(dplyr)
library(tidyr)
library(ggplot2)

df <- data.frame(A = rep(c("Group 1","Group 2"),each=3),
                 B = c("possum",rep("dog",2),rep("possum",3)))

df
#>         A      B
#> 1 Group 1 possum
#> 2 Group 1    dog
#> 3 Group 1    dog
#> 4 Group 2 possum
#> 5 Group 2 possum
#> 6 Group 2 possum

ggplot(df, aes(x=A,fill=B)) +
  geom_bar(position = position_dodge(width = -0.9, preserve = "single"))

解决这个问题的方法是手动计数:

df_tally <- df %>% 
  group_by(A,B) %>% 
  tally() %>% 
  ungroup()

df_tally
#>   A       B          n
#> 1 Group 1 dog        2
#> 2 Group 1 possum     1
#> 3 Group 2 possum     3

然后我们需要添加缺失的组合:

df_tally <- complete(df_tally, A, B)

df_tally
#>   A       B          n
#> 1 Group 1 dog        2
#> 2 Group 1 possum     1
#> 3 Group 2 dog       NA
#> 4 Group 2 possum     3

现在,因为我们已经完成了计数,所以我们使用 stat = "identity":

ggplot(df_tally, aes(x=A, y=n, fill=B)) +
  geom_bar(stat = "identity",
           position = position_dodge(width = -0.9, preserve = "single"))
#> Warning: Removed 1 rows containing missing values (geom_bar).

在您的情况下,这看起来像:

DATA3 <- DATA2 %>% 
  group_by(A,B,C) %>% 
  tally() %>% 
  ungroup() %>% 
  complete(A,B,C)


ggplot(DATA3, aes(x=C,y=n,fill=B)) +
  geom_bar(stat="identity", 
           position = position_dodge(width = -0.9, preserve = "single")) +
  facet_grid(.~A) +
  theme_bw() + 
  scale_fill_manual(values = c("grey","grey40")) + 
  coord_flip()
#> Warning: Removed 2 rows containing missing values (geom_bar).

更新:dplyr 0.8.0

截至dplyr 0.8.0 , group_by 具有 .drop 参数,用于维护没有任何数据的因素的组。这稍微简化了必要的代码。示例:

DATA3 <- DATA2 %>% 
  group_by(A,B,C, .drop = FALSE) %>% 
  summarise(n = n())

关于r - 条形图 (ggplot2) 中的单个条形图错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484592/

相关文章:

r - crosstalk的filter_select不会过滤图表

r - 如何使用单独的 R 中的输入创建 Shiny 的 react 函数

r - ggplot2:geom_text 使用绘图调整大小,并在 geom_bar 中强制/适合文本

r - 更改 ggplot geom_bar 中 NA 值的标签和颜色?

r - 将光标移动到条形图上时,带有 geom_bar 的 ggplotly 显示错误的 y 轴值

r - 如何用向量运算替换双 for 循环

r - 如何在 R 中对列联表重新排序以形成混淆矩阵

r - 棘手函数的数值积分

arrays - 将值与R中另一个数组中的最近值匹配

r - 如何删除堆叠的 geom_col 之间的空白