如何在 ggplot2 中构建因子均值图(多因子方差分析)

问题描述 投票:0回答:1

我正在执行 L9 正交设计,为了直观地查看不同因子水平的情况,我有兴趣构建均值图。使用代码:

plot.design(PCR ~ Temperature+Time+Ratio+Pretreatment, data = scenedesmus)

我得到:

enter image description here

我想用 ggplot2 改进这张图,但我做不到。此外,如果每个因素的不同水平彼此略微分开,每个因素有不同的颜色,并且包含每个平均因素的标准差,那就太好了。我独自获得了 ggplot2 中的每一个图表,但我不知道如何合并它们:

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())

enter image description here

enter image description here

我想获得这样的东西:

enter image description here

我的数据集如下:

> 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
r ggplot2 graph anova
1个回答
0
投票

您需要将所有因素置于一个文本列中,并将它们绘制在 x 轴上。

library(tidyverse)

scenedesmus %>%
  mutate(across(Temperature:Pretreatment, as.character)) %>%
  pivot_longer(Temperature:Pretreatment) %>%
  mutate(value = factor(value, levels(factor(a$value))[
    c(10:12, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  ggplot(aes(value, PCR, group = name, color = name)) +
  geom_point(stat = 'summary', fun = mean) +
  geom_errorbar(stat = 'summary', width = 0.1, alpha = 0.5) +
  geom_line(stat = 'summary', fun = mean) +
  geom_text(stat = 'summary', fun = mean, color = 'black',
            aes(label = after_stat(round(y, 2))),
            position = position_nudge(0.4, 0.01)) +
  facet_grid(~name, scales = 'free_x', switch = 'x') +
  scale_color_brewer(palette = 'Set1', guide = 'none') +
  coord_cartesian(clip = 'off') +
  geom_vline(data = data.frame(a = 0.4, name = 'Pretreatment'),
             aes(xintercept = a)) +
  theme_minimal(base_size = 20) +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        axis.title.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.spacing.x = unit(0, 'mm'),
        axis.ticks = element_line(),
        axis.line.x = element_line())

© www.soinside.com 2019 - 2024. All rights reserved.