当值为空时,Ggplot2 错误地重新排列绘图栏中的条形图

Ggplot2 rearranges wrongly the bars in a plot bar when value is null

给定以下数据,我用一个因子和一个数字列组成了一个数据框。

X2 <- c(4,4,3,5,4,4,2,3,4,3,5,5,4,3,3,4,2,3,3,4,3,5,3,3,4,4,3,3,5,4,5,4,4,3,5,5,3,5,4,5,5,4,4,2,3,3,3,4,4,4,2,4,4,4,4,4,2,4,4,3,3,3,5,3,4,3,3,4,4,4,4,1,3,3,4,3,3,2,4,1)
X3 <- rep("I",40)
X4 <- rep("C",40)
Group <- c(X3,X4)
dat2 <- data.frame(X2,Group)
dat2$Group <- factor(dat2$Group)
levels(dat2$Group) = c("I","C")
Group <- c("C","I")
grp.mean <- c(3.8,3.375)
mu2 <- data.frame(Group,grp.mean)

我想用均值处的垂直线绘制以下条形图,这是我的代码:

p2 <-ggplot(dat2, aes(x=X2))+
  geom_bar(aes(color=Group,fill=Group),alpha=0.4, position= position_dodge(preserve = "single"))+
  geom_vline(data=mu2, aes(xintercept=grp.mean, color=Group), linetype="dashed")+
  xlab("Density in Responses") + 
  ylab("Levels")+
  theme_gray() +
  theme_grey(base_size = 30)+
  theme(axis.text=element_text(size=22),
        axis.title=element_text(size=20,face="bold"),
        legend.title=element_text(size=16),
        legend.text=element_text(size=14))+
  theme(plot.title = element_text(hjust = 0.5,size=19,face="bold"))

p2

我得到了一个情节,它检查了我所有的期望,除了一个。当我有一个条件(C和I)为空的值时,它会自动更改位置,我不知道为什么!根据我的逻辑,它应该保持在相同的位置并在正确的位置绘制条形图。我附上一张图片,这样你就可以看到发生了什么。

如您所见,在没有红色条的情况下,蓝色条取代了红色条(因为它的值为 0)。有谁知道为什么会这样,有什么办法可以解决这个问题吗?

谢谢!

一个work-around是对ggplot之前的观察次数进行统计,并将统计信息绘制出来

请注意,我在您的第一个 Group 向量中交换了 X3X4,因此红色在左边,蓝色在右边。

library(tidyverse)
X2 <- c(4,4,3,5,4,4,2,3,4,3,5,5,4,3,3,4,2,3,3,4,3,5,3,3,4,4,3,3,5,4,5,4,4,3,5,5,3,5,4,5,5,4,4,2,3,3,3,4,4,4,2,4,4,4,4,4,2,4,4,3,3,3,5,3,4,3,3,4,4,4,4,1,3,3,4,3,3,2,4,1)
X3 <- rep("I",40)
X4 <- rep("C",40)
Group <- c(X4,X3)
dat2 <- data.frame(X2,Group)
dat2$Group <- factor(dat2$Group)
levels(dat2$Group) = c("I","C")
Group <- c("C","I")
grp.mean <- c(3.8,3.375)
mu2 <- data.frame(Group,grp.mean)

dat2 %>% group_by(X2, Group) %>% summarize(n = n()) %>% complete(Group, fill = list(n = 0)) %>% 
  ggplot(aes(x=X2, n))+
  geom_bar(aes(color=Group,fill=Group),alpha=0.4, position= position_dodge(), stat = "identity")+
  geom_vline(data=mu2, aes(xintercept=grp.mean, color=Group), linetype="dashed")+
  xlab("Density in Responses") + 
  ylab("Levels")+
  theme_gray() +
  theme_grey(base_size = 30)+
  theme(axis.text=element_text(size=22),
        axis.title=element_text(size=20,face="bold"),
        legend.title=element_text(size=16),
        legend.text=element_text(size=14))+
  theme(plot.title = element_text(hjust = 0.5,size=19,face="bold"))
#> `summarise()` has grouped output by 'X2'. You can override using the `.groups`
#> argument.

reprex package (v2.0.1)

创建于 2022-05-13

像其他评论者一样,我也得到了相反的条形颜色,所以我更改了 I/C 值,它与你的相匹配。 我不确定我的结果是否会让你满意,因为我设法让蓝色条填满 X=1

的整个 space

无论如何,我还使用了更清晰的代码来生成 table:

X2 <- c(4,4,3,5,4,4,2,3,4,3,5,5,4,3,3,4,2,3,3,4,3,5,3,3,4,4,3,3,5,4,5,4,4,3,5,5,3,5,4,5, #40
        5,4,4,2,3,3,3,4,4,4,2,4,4,4,4,4,2,4,4,3,3,3,5,3,4,3,3,4,4,4,4,1,3,3,4,3,3,2,4,1)
# First col is the unique X2 values.
# Second col is Group, which is a factor. It is a repetition of I/C, each 40 times (40*I and then 40*C)
# grp.mean is a grouped mean (by each Group[I/C]) of X2.
dat2 <- data.frame(
  X2,
  Group=factor(rep(c("C","I"),each=40))
) %>% group_by(Group) %>% mutate(grp.mean=mean(X2)) %>% ungroup()

dat2 %>% ggplot(aes(X2,fill=Group))+
  geom_bar(position="dodge")+
  geom_vline(xintercept = dat2$grp.mean)