給定以下資料,我用一個因子和一個數字列組成一個資料框。
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)。有誰知道為什么會發生這種情況,有什么辦法可以解決這個問題嗎?
謝謝!
uj5u.com熱心網友回復:
一種解決方法是計算 之前的觀察次數ggplot,并繪制計數資訊。
請注意,我已在您的第一個向量中交換了X3和,因此紅色在左側,藍色在右側。X4Group
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 包(v2.0.1)創建于 2022-05-13
uj5u.com熱心網友回復:
像其他評論者一樣,我也得到了相反的條形顏色,所以我更改了 I/C 值,它與你的匹配。我不確定我的結果是否會讓你滿意,因為我設法讓藍色條填滿 X=1 的整個空間

無論如何,我還使用了更簡潔的代碼來生成表格:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474395.html
