使用 R 中的 ggplot2 比较女性和男性数据以及所有数据的箱线图

Boxplot comparing female and male data alongside ALL data using ggplot2 in R

我想比较雌性和雄性动物对不同光处理(光)的反应 (RRP) 以及总组合数据。 我用过:

emRRP <- melt(emper, id=c("Sex", "RRP", "Light"))

data.frame(emRRP) Sex RRP Light 1 F 0.63916773 AA30 2 F 0.71016609 AA30 3 F 0.40279640 AA30 4 F 0.69812010 AA30 17 M 0.76417910 AA30 18 M 0.71837927 AA30 19 M 0.95454545 AA30 20 M 0.69392225 AA30

我可以绘制女性和男性数据:

emRRPLT <- factor(
    emRRP$Light, 
    levels = c("LD", "LA05", "LA30", "LA50", "LA80", "AA30", "LL", "DD")
)

ggplot(emRRP, aes(x=emRRPLT, y=RRP, color=Sex)) +
   geom_boxplot() + 
   theme_classic() + 
   labs(x="Light treatment")

Boxplot of female vs male responses

但不知道如何添加组合数据。

如果您提供实际数据的子集而不是图片会更好,以便我们测试解决方案。无论如何,你应该能够通过将数据集附加到自身并创建一个新的分组变量来做你想做的事:

library(dplyr)
library(ggplot2)
mutate(emRRPLT, group=SEX) %>%
   bind_rows(mutate(emRRPLOT, group="All")) %>%
   ggplot(aes(x=emRRPLT, y=RRP, color=group)) +
   geom_boxplot() + 
   theme_classic() + 
   labs(x="Light treatment")