单个条放错在条形图(ggplot2)中

时间:2019-02-01 17:43:26

标签: r ggplot2 geom-bar

我正在做2个面板barplot,但似乎是具有单条是它的位置的出了问题。它是在右面板的上部的杆。填充为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, 2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, 3L, 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)在右侧面板的上部被放错地方,虽然填充是正确的。有小费吗?谢谢!

1 个答案:

答案 0 :(得分:2)

有一些变量值组合不会显示在数据中,即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())