如何用ggplot2绘制多因素ANOVA因子均值图(L9正交设计)
正交设计因子均值优化绘图方案(ggplot2实现)
需求说明
我正在进行L9正交设计,为直观查看不同因子水平对PCR(蛋白-碳水化合物比)的影响,先用plot.design得到了基础图:
plot.design(PCR ~ Temperature+Time+Ratio+Pretreatment, data = scenedesmus)
现在需要用ggplot2优化该图,满足以下要求:
- 各因子的水平分组稍作分离
- 按因子区分颜色
- 纳入每个因子水平均值的标准差
目前我能单独绘制单个因子的ggplot2图,但不知道如何将多个因子的图合并成类似plot.design的组合图,单因子绘图代码如下:
ggline(scenedesmus, x = "Temperature", y = "PCR", add = "mean_sd", size=1, ylab ="Protein-carbohydrate ratio", legend="right", error.plot = "errorbar") + theme(axis.text=element_text(size=20), axis.title=element_text(size=24,face="bold"), legend.title=element_text(size=24, face="bold"), legend.text=element_text(size=22))+ scale_x_discrete(labels = scales::label_parse())
数据集:
> scenedesmus Temperature Time Ratio Pretreatment PRY CRY 1 20 0.5 3 None 7.106190 12.99137 2 20 0.5 3 None 6.991073 13.20371 3 20 1 6 Mortar 9.816545 14.62239 4 20 1 6 Mortar 10.093768 14.41567 5 20 2 12 Discs 15.887290 20.85106 6 20 2 12 Discs 16.514740 21.13347 7 30 0.5 6 Discs 15.608507 20.75174 8 30 0.5 6 Discs 15.890457 20.37846 9 30 1 12 None 9.851556 13.19030 10 30 1 12 None 10.329157 12.74816 11 30 2 3 Mortar 9.815574 14.37999 12 30 2 3 Mortar 10.177421 15.15487 13 40 0.5 12 Mortar 12.097258 16.36536 14 40 0.5 12 Mortar 11.135055 17.34924 15 40 1 3 Discs 14.759191 22.44141 16 40 1 3 Discs 14.884651 22.43402 17 40 2 6 None 9.476980 14.08952 18 40 2 6 None 10.832856 16.03889 PCR 1 0.5469931 2 0.5294781 3 0.6713366 4 0.7001942 5 0.7619416 6 0.7814496 7 0.7521542 8 0.7797673 9 0.7468787 10 0.8102468 11 0.6825854 12 0.6715611 13 0.7391992 14 0.6418181 15 0.6576767 16 0.6634856 17 0.6726260 18 0.6754117
解决方案
步骤1:数据预处理(宽转长格式)
首先将数据集转换为长格式,方便ggplot2批量处理所有因子:
library(tidyverse) # 转换为长格式,保留PCR作为响应变量 scenedesmus_long <- scenedesmus %>% select(Temperature, Time, Ratio, Pretreatment, PCR) %>% pivot_longer(cols = -PCR, names_to = "Factor", values_to = "Level") %>% # 将Level转为字符型,统一类型方便绘图 mutate(Level = as.character(Level))
步骤2:计算各因子水平的均值和标准差
提前统计每个因子-水平组合的均值、标准差,避免绘图时重复计算:
summary_stats <- scenedesmus_long %>% group_by(Factor, Level) %>% summarise( mean_PCR = mean(PCR), sd_PCR = sd(PCR), .groups = "drop" )
步骤3:ggplot2绘制组合图
使用facet_wrap实现多因子图的合并,同时满足所有需求:
library(ggplot2) ggplot(summary_stats, aes(x = Level, y = mean_PCR, color = Factor)) + # 绘制标准差误差棒 geom_errorbar(aes(ymin = mean_PCR - sd_PCR, ymax = mean_PCR + sd_PCR), width = 0.2, size = 1) + # 绘制均值点 geom_point(size = 4) + # 按因子分面,每个因子单独成图 facet_wrap(~Factor, scales = "free_x") + # 设置坐标轴标签 labs( y = "Protein-carbohydrate ratio", x = "Level", color = "Factor" ) + # 匹配单因子图的主题样式 theme_bw() + theme( axis.text = element_text(size = 20), axis.title = element_text(size = 24, face = "bold"), legend.title = element_text(size = 24, face = "bold"), legend.text = element_text(size = 22), strip.text = element_text(size = 22, face = "bold") # 分面标题样式 ) + # 可选:添加原始数据点并轻微分离,增强直观性 geom_jitter(data = scenedesmus_long, aes(x = Level, y = PCR), alpha = 0.5, size = 2, width = 0.1)
代码说明
pivot_longer:将宽格式数据转为长格式,把所有因子列合并为Factor和Level两列,便于统一处理。group_by + summarise:统计每个因子水平的均值和标准差,用于绘制误差棒。facet_wrap(~Factor, scales = "free_x"):按因子分面,每个因子的图单独展示,x轴自动适配不同因子的水平。color = Factor:按因子区分颜色,每个因子的点和误差棒用不同颜色显示。geom_jitter:添加原始数据点并设置width=0.1,实现水平方向的轻微分离,避免数据点重叠。
内容的提问来源于stack exchange,提问作者David Moldes
相关产品推荐
相关产品推荐

