我想制作一个条形图,其中包含两个时间点“之前”和“之后”的数据叠加。
在每个时间点,参与者都会被问到两个问题(“疼痛”和“恐惧”),他们会通过给出 1、2 或 3 的分数来回答。
我现有的代码很好地绘制了“之前”时间点的数据计数,但我似乎无法添加“之后”数据的计数。
这是我希望绘图在添加“之后”数据后的样子的草图,黑条代表“之后”数据:
我想在 ggplot2() 中绘制图,并且我尝试改编来自如何在 R 中叠加条形图的代码? 但我无法让它用于分组数据。
非常感谢!
#DATA PREP
library(dplyr)
library(ggplot2)
library(tidyr)
df <- data.frame(before_fear=c(1,1,1,2,3),before_pain=c(2,2,1,3,1),after_fear=c(1,3,3,2,3),after_pain=c(1,1,2,3,1))
df <- df %>% gather("question", "answer_option") # Get the counts for each answer of each question
df2 <- df %>%
group_by(question,answer_option) %>%
summarise (n = n())
df2 <- as.data.frame(df2)
df3 <- df2 %>% mutate(time = factor(ifelse(grepl("before", question), "before", "after"),
c("before", "after"))) # change classes and split data into two data frames
df3$n <- as.numeric(df3$n)
df3$answer_option <- as.factor(df3$answer_option)
df3after <- df3[ which(df3$time=='after'), ]
df3before <- df3[ which(df3$time=='before'), ]
# CODE FOR 'BEFORE' DATA ONLY PLOT - WORKS
ggplot(df3before, aes(fill=answer_option, y=n, x=question)) + geom_bar(position="dodge", stat="identity")
# CODE FOR 'BEFORE' AND 'AFTER' DATA PLOT - DOESN'T WORK
ggplot(mapping = aes(x, y,fill)) +
geom_bar(data = data.frame(x = df3before$question, y = df3before$n, fill= df3before$index_value), width = 0.8, stat = 'identity') +
geom_bar(data = data.frame(x = df3after$question, y = df3after$n, fill=df3after$index_value), width = 0.4, stat = 'identity', fill = 'black') +
theme_classic() + scale_y_continuous(expand = c(0, 0))
Run Code Online (Sandbox Code Playgroud)
我认为,线索是设置了width的酒吧“后”,但躲闪他们,就好像他们的宽度0.9(即相同的(默认)宽度“之前”吧)。另外,因为我们没有映射 fill“after”条,所以我们需要使用group审美来实现躲避。
我更喜欢只有一个数据集,并且在每次调用geom_col.
ggplot(mapping = aes(x = question, y = n, fill = factor(ans))) +
geom_col(data = d[d$t == "before", ], position = "dodge") +
geom_col(data = d[d$t == "after", ], aes(group = ans),
fill = "black", width = 0.5, position = position_dodge(width = 0.9))
Run Code Online (Sandbox Code Playgroud)

数据:
set.seed(2)
d <- data.frame(t = rep(c("before", "after"), each = 6),
question = rep(c("pain", "fear"), each = 3),
ans = 1:3, n = sample(12))
Run Code Online (Sandbox Code Playgroud)
使用 的替代数据准备data.table,从您原来的“df”开始:
library(data.table)
d <- melt(setDT(df), measure.vars = names(df), value.name = "ans")
d[ , c("t", "question") := tstrsplit(variable, "_")]
Run Code Online (Sandbox Code Playgroud)
要么预先计算计数并按上述方式继续 geom_col
# d2 <- d[ , .N, by = .(question, ans)]
Run Code Online (Sandbox Code Playgroud)
或者让我们geom_bar计算:
ggplot(mapping = aes(x = question, fill = factor(ans))) +
geom_bar(data = d[d$t == "before", ], position = "dodge") +
geom_bar(data = d[d$t == "after", ], aes(group = ans),
fill = "black", width = 0.5, position = position_dodge(width = 0.9))
Run Code Online (Sandbox Code Playgroud)
数据:
df <- data.frame(before_fear = c(1,1,1,2,3), before_pain = c(2,2,1,3,1),
after_fear = c(1,3,3,2,3),after_pain = c(1,1,2,3,1))
Run Code Online (Sandbox Code Playgroud)