【问题标题】:Adjusted regression line considering different factors in ggplot2考虑ggplot2中不同因素的调整回归线
【发布时间】:2021-04-22 02:45:23
【问题描述】:

我正在尝试重现下图,其中内部线是调整后的回归线:

但是,由于某些因素,它没有被绘制成应有的样子,也就是说,只呈现了一条线,而且,变量“teor”的不同浓度没有像图像中那样被绘制出来上面,看下面的结果:

dados = read.table("datanew.csv", header = T, sep=";", dec=","); head(dados)
dados$Trat <- factor(dados$Trat)
dados$Teor <- factor(dados$Teor)
dadosnew$Tempo = as.factor(dadosnew$Tempo)

my.formula <- y ~ x
p = ggplot(dadosnew, aes(x = Tempo, y = massaseca, group = Fator)) +
  stat_summary(geom = "point", fun = mean) + 
  stat_smooth(method = "lm", se=FALSE,  formula=y ~ poly(x, 2, raw=TRUE)) + stat_poly_eq(formula = my.formula,
                                                                                         eq.with.lhs = "As-italic(hat(y))~`=`~",
                                                                                         aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), 
                                                                                         parse = TRUE, size = 5, label.y = 35)+
  labs(title = "",
       x = "Time (Minutes)",
       y = "Weight (mg)") + theme_bw() +
  theme(axis.title = element_text(size = 23,color="black"),
        axis.text = element_text(size = 18,color="black"),
        text = element_text(size = 50,color="black"),
        legend.position = "none") + facet_wrap(~Fator)
p 

【问题讨论】:

    标签: r ggplot2 plot regression linear-regression


    【解决方案1】:

    你需要另一个像这样的分组变量:

    #Code
    my.formula <- y ~ x
    ggplot(dadosnew, aes(x = Tempo, y = massaseca, group = interaction(Fator,Trat))) +
      stat_summary(geom = "point", fun = mean) + 
      stat_smooth(method = "lm", se=FALSE,  formula=y ~ poly(x, 2, raw=TRUE)) +
      stat_poly_eq(formula = my.formula,eq.with.lhs = "As-italic(hat(y))~`=`~",
                   aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
                   parse = TRUE, size = 5, label.y = 35)+
      labs(title = "",
           x = "Time (Minutes)",
           y = "Weight (mg)") + theme_bw() +
      theme(axis.title = element_text(size = 23,color="black"),
            axis.text = element_text(size = 18,color="black"),
            text = element_text(size = 50,color="black"),
            legend.position = "none") + facet_wrap(~Fator)
    

    输出:

    作为研究人员,您应该定义什么。

    【讨论】:

    • group=Teorfacet_wrap(~Fator) 得到相同的结果
    • @YBS 是的,只需要添加或更改分组变量。非常感谢!
    • @Duck 如何根据第一张图中所需图表中建立的时间将不同线条的图例和平均值包含在平均值中?
    • @BrenoG。先在aes()中添加颜色声明,比如color=interaction(Fator,Trat)我会尽快回复你!
    • @BrenoG。请删除theme()legend.positiontext 中的这些选项,您将拥有您想要的图例。亲切的问候!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2014-02-23
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多