【发布时间】:2021-01-11 11:46:59
【问题描述】:
我是 R 的新手。我没有足够的经验来了解我应该如何格式化我的数据以生成多个图表来比较 R 中的某些组。 我有两个时间点进行 3 次治疗和 2 次对照。 我希望能够创建多个图表来比较特定组。 T1和T2是时间点。
test <- structure(list(group = c("control1 T1", "control2 T1", "treatment1 T1",
"treatment2 T1", "treatment3 T1", "control1 T1", "control2 T1",
"treatment1 T1", "treatment2 T1", "treatment3 T1", "control1 T1",
"control2 T1", "treatment1 T1", "treatment2 T1", "treatment3 T1",
"control1 T2", "control2 T2", "treatment1 T2", "treatment2 T2",
"treatment3 T2", "control1 T1", "control2 T1", "treatment1 T1",
"treatment2 T1", "treatment3 T1", "control1 T1", "control2 T1",
"treatment1 T1", "treatment2 T1", "treatment3 T1", "control1 T1",
"control2 T1", "treatment1 T1", "treatment2 T1", "treatment3 T1",
"control1 T2", "control2 T2", "treatment1 T2", "treatment2 T2",
"treatment3 T2"), value = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 4L, 5L, 6L)), class = "data.frame", row.names = c(NA,
-40L))
我试过这个:
my_comparisons <- list( c("control1 T1", "control1 T2"), c("control2 T1", "control2 T2"), c("treatment1 T1", "treatment1 T2") , c("treatment3 T1", "treatment3 T2"))#
ggboxplot(test, x = "group", y = "value", color = "group",
#add = "jitter",
legend = "none", outlier.shape = NA) +
rotate_x_text(angle = 45) + geom_jitter(width = 0.15, alpha = .1, color = "black") +
stat_compare_means(comparisons = my_comparisons, label.y = c(5, 5, 5, 5, 5, 5))+
stat_compare_means(label.y = 5)
上述 ggboxplot 生成的图表很好,但我想将特定组相互比较。例如“治疗1 T1”、“治疗1 T2”。
我试过 facet_wrap。
p <- ggplot(data = test, aes(x=group, y=value)) +
geom_boxplot(aes(fill=group))
p + facet_wrap( ~ group, scales="free")
我喜欢这种格式,但我每个区域只有一个图表。理想情况下,我想在每个部分中比较两组。我不知道该怎么做。我可以手动拆分数据并一次制作每个图表,但应该可以一次完成所有操作并选择要针对每个方面比较哪些组?
【问题讨论】: