R 用误差条绘制变异性的线性回归分析图

R 用误差条绘制变异性的线性回归分析图,r,ggplot2,linear-regression,errorbar,R,Ggplot2,Linear Regression,Errorbar,我想让绘图看起来像图1(来源:) 在图1中,他们绘制了一年产量变化的回归分析图。在我的案例中,我想为每个治疗组绘制两个位置和4个区块之间的变化。因此,我想要的绘图将包含三个方面的因子B.葡萄糖苷酶,蛋白质,变量的POX.C,以及四种颜色的因子。此外,在我目前的情节中,我有关于阻塞和处理的图例。我应该只进行治疗,因为块应该用于制作可变性的误差条 我试过使用这段代码,但显然不符合我的要求。(以下包括df.melled的数据。) 数据: df 汇总数据框(这也可以通过stat\u summary()

我想让绘图看起来像图1(来源:)

在图1中,他们绘制了一年产量变化的回归分析图。在我的案例中,我想为每个治疗组绘制两个位置和4个区块之间的变化。因此,我想要的绘图将包含三个方面的因子
B.葡萄糖苷酶
蛋白质
变量
POX.C
,以及四种颜色的因子。此外,在我目前的情节中,我有关于阻塞和处理的图例。我应该只进行治疗,因为块应该用于制作可变性的误差条

我试过使用这段代码,但显然不符合我的要求。(以下包括
df.melled
的数据。)

数据:

df
汇总数据框(这也可以通过
stat\u summary()
来完成,但在前面明确地完成通常更清晰/更透明)。(我认为,由于数据集是平衡的,您可以先在块结构上进行折叠/平均,然后使用缩减后的数据集绘制整个图-它不应该改变线性回归的结果,至少不应该改变平均值…而且任何统计比较都应该在块级摘要上进行。)。。)

(将
group=1
添加到
stat\u poly\u eq()
美学意味着我们仅绘制每个方面的单个R^2值)


<> P>因为你不再使用任何形状的美学,你可以考虑使用它来显示位置变量…

你能提供更多关于你如何计算错误条的信息吗?@ Caldial40我做的方式是不正确的。首先应按相同的处理组(包括两个位置和所有四个块)对其进行分组,然后计算误差条。以下是一个初始运行,在删除块的图例时获取颜色和形状:
df.melled%%>%mutate(块=因子(块),处理=因子(处理))%%>%ggplot(aes(x=值,y=产量))+几何点(aes(颜色=治疗,形状=块))+几何平滑(方法=“lm”,se=假,颜色=“深灰色”)+镶嵌面包裹(~variable,scales=“free”)+缩放形状(guide=“none”)
那么你想得到八个独立的平均值和标准差计算,每个位置/块对一个吗?@cardinal40我想你很接近了,但是我们可以为这些形状设置误差条吗?所以误差条由4个形状组成。谢谢你的回答。一个问题-你如何计算位置?有8个data点在你的图中,但我只想要4点,每个治疗一个点,每个面只有一个R平方值。我认为这解决了8个数据点
df.sum%groupby(治疗,变量)%%>%summary(值=平均值,收益率)的问题,##在计算sd!yield=mean(yield))之后,将收益率折叠为mean*。
,但我仍然只希望每个方面有一个R-sq值。非常感谢您的帮助。
ggplot(df.melted, aes(x = value, y = yield, color = as.factor(treatment))) + 
  geom_point(aes(shape= as.factor(block))) +
  stat_smooth(method = "lm", formula = y ~ x, col = "darkslategrey", se=F) +
  stat_poly_eq(formula = y~x, 
               # aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
               aes(label =  ..rr.label..), 
               parse = TRUE) + 
  theme_classic() +
  geom_errorbar(aes(ymax = df.melted$yield+sd(df.melted$yield), ymin = df.melted$yield-sd(df.melted$yield)), width = 0.05)+
facet_wrap(~variable) 
df.melted <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("M", "U"), class = "factor"), 
    treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("CC", 
    "CCS", "CS", "SCS"), class = "factor"), block = c(1L, 2L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L), yield = c(5156L, 5157L, 5551L, 5156L, 4804L, 
    4720L, 4757L, 5021L, 4826L, 4807L, 4475L, 4596L, 4669L, 4588L, 
    4542L, 4592L, 5583L, 5442L, 5693L, 5739L, 5045L, 4902L, 5006L, 
    5086L, 4639L, 4781L, 4934L, 4857L, 4537L, 4890L, 4842L, 4608L, 
    5156L, 5157L, 5551L, 5156L, 4804L, 4720L, 4757L, 5021L, 4826L, 
    4807L, 4475L, 4596L, 4669L, 4588L, 4542L, 4592L, 5583L, 5442L, 
    5693L, 5739L, 5045L, 4902L, 5006L, 5086L, 4639L, 4781L, 4934L, 
    4857L, 4537L, 4890L, 4842L, 4608L, 5156L, 5157L, 5551L, 5156L, 
    4804L, 4720L, 4757L, 5021L, 4826L, 4807L, 4475L, 4596L, 4669L, 
    4588L, 4542L, 4592L, 5583L, 5442L, 5693L, 5739L, 5045L, 4902L, 
    5006L, 5086L, 4639L, 4781L, 4934L, 4857L, 4537L, 4890L, 4842L, 
    4608L), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("B.glucosidase", 
    "Protein", "POX.C"), class = "factor"), value = c(1.600946, 
    1.474084, 1.433078, 1.532492, 1.198667, 1.193193, 1.214941, 
    1.360981, 1.853056, 1.690117, 1.544357, 1.825132, 1.695409, 
    1.764123, 1.903743, 1.538684, 0.845077, 1.011463, 0.857032, 
    0.989803, 0.859022, 0.919467, 1.01717, 0.861689, 0.972332, 
    0.952922, 0.804431, 0.742634, 1.195837, 1.267285, 1.08571, 
    1.20097, 6212.631579, 5641.403509, 4392.280702, 7120.701754, 
    5305.964912, 4936.842105, 5383.157895, 6077.894737, 5769.122807, 
    5016.842105, 5060.350877, 5967.017544, 5576.842105, 5174.035088, 
    5655.438596, 5468.77193, 7933.333333, 7000, 6352.982456, 
    8153.684211, 6077.894737, 4939.649123, 5002.807018, 6489.122807, 
    4694.035088, 5901.052632, 4303.859649, 6768.421053, 6159.298246, 
    6090.526316, 4939.649123, 5262.45614, 810.3024, 835.5242, 
    856.206, 759.8589, 726.2298, 792.6472, 724.7165, 699.3266, 
    500.9153, 634.8698, 637.9536, 648.8814, 641.0357, 623.3822, 
    555.2834, 520.8119, 683.3528, 595.9173, 635.4315, 672.4234, 
    847.2944, 745.5665, 778.3548, 735.8141, 395.2647, 570.4148, 
    458.0383, 535.3851, 678.0293, 670.7419, 335.2923, 562.5674
    )), row.names = c(NA, -96L), class = "data.frame")
library(dplyr)
library(ggplot2)
library(ggpmisc)
df.sum <- (df.melted
    %>% group_by(Location,treatment,variable)
    %>% summarise(value=mean(value),yield_sd=sd(yield),
                  ## collapse yield to mean *after* computing sd!
                  yield=mean(yield))
)
(ggplot(df.melted,
       aes(x = value, y = yield, color = treatment))
    + stat_smooth(method = "lm", col = "darkslategrey", se=FALSE)
    + stat_poly_eq(
          formula = y ~ x,
          ## aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
          aes(group=1, label =  ..rr.label..), 
          parse = TRUE)
    + theme_classic()
    + scale_shape(guide=FALSE)
    + geom_point(data=df.sum)
    + geom_errorbar(data=df.sum,
                    aes(ymax = yield+yield_sd, ymin = yield-yield_sd),
                    width = 0.05)
    + facet_wrap(~variable,scale="free_x")
)