我有这个数据集,但我正在努力解决 2 个问题。
数据集的快照是这样的:
Score<-c(-2, 3, 4, -1, 3, 4, 5, -1, 3, 5, -3, 3, 5, 1, -4, 5, -2,
1, 3, 4, -4, 2, -1, 3, 4, -2, 3, 4, -1, 3, 4, 5, -1, 3, 5, -3, 3, 5, 1, -4, 5, -2,
1, 3, 4, -4, 2, -1, 3, 4)
Group<-c( "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "A", "S", "A",
"S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "A", "S", "A",
"S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S"
)
Scenerio_ID <-c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
CombinedTable<-data.frame(Score,Group, Scenerio_ID)
我有这段代码可以生成我想要的图形类型:
CombinedTable %>%
mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
arrange(Scenerio_ID) %>%
ggplot(aes(x = factor(Scenerio_ID), y = avg_Score, color = ifelse(avg_Score > 0, "U", "T"))) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
labs(x = "A or S Per Scenerio", y = "Score", color = "") +
facet_wrap(~ Scenerio_ID, nrow = 1L, strip.position = "bottom") +
theme_classic() +
scale_color_manual(values = c("U" = "blue", "T" = "red"))
我正在尝试做两件事:
我希望能够将所有场景放在同一个图中,但为了让场景有序,由于某种原因,x 轴上场景的顺序似乎是随机排列的。
我想创建一个只有特定场景的图表(例如,4、10、16、17、19)。
非常感谢以上两点的任何帮助。我的尝试如下:
selected_scenarios <- c("4", "10", "16", "17", "19")
MeansCombinedTable %>%
filter(Scenerio_ID %in% selected_scenarios) %>%
ggplot(aes(x = Group, y = avg_Score)) +
geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
labs(x = "", y = "Score") +
facet_wrap(~ Scenerio_ID, ncol = 5, scales = "free_x") +
theme_classic()+ scale_color_manual(values = c("U" = "blue", "T" = "red"))
添加了 dplyr summarize 的这段代码仍然不起作用:
mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
group_by(Group, Scenerio_ID) %>%
dplyr::summarise(avg_Score = mean(Score),
lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
uci_Score = mean(Score) + 1.96 * sd(Score)/sqrt(n()),
col = ifelse(avg_Score > 0, "U", "T")) %>%
ggplot(aes(Scenerio_ID, y = avg_Score, color = col)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
labs(x = "A or S Per Scenerio", y = "Score", color = "") +
theme_classic() +
scale_color_manual(values = c("U" = "blue", "T" = "red"))```
给定的数据,只要正确分组归纳应该没问题。显然没有足够的测量来获得样本数据中所有点的置信区间:
CombinedTable %>%
mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
group_by(Group, Scenerio_ID) %>%
summarize(avg_Score = mean(Score),
lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
uci_Score = mean(Score) + 1.96 * sd(Score)/sqrt(n()),
col = ifelse(avg_Score > 0, "U", "T")) %>%
ggplot(aes(Scenerio_ID, y = avg_Score, color = col)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
labs(x = "A or S Per Scenerio", y = "Score", color = "") +
theme_classic() +
scale_color_manual(values = c("U" = "blue", "T" = "red"))
如果你只想选择几个进行绘图,请使用
summarize
和 %in%
:
CombinedTable %>%
mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
group_by(Group, Scenerio_ID) %>%
summarize(avg_Score = mean(Score),
lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
uci_Score = mean(Score) + 1.96 * sd(Score)/sqrt(n()),
col = ifelse(avg_Score > 0, "U", "T")) %>%
filter(Scenerio_ID %in% c(1, 4, 5)) %>%
ggplot(aes(Scenerio_ID, y = avg_Score, color = col)) +
geom_point(size = 2) +
geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
labs(x = "A or S Per Scenerio", y = "Score", color = "") +
theme_classic() +
scale_color_manual(values = c("U" = "blue", "T" = "red"))