简体   繁体   English

如何将不同回归的 R2 和回归方程放在一张图中?

[英]How to put R2 and regression equation from different regression in one graph?

I have example data set below since I couldn't put all data set here:我在下面有示例数据集,因为我无法将所有数据集放在这里:

structure(list(start_date = structure(c(1488153600, 1488153600, 
1488758400, 1488758400, 1489363200, 1489363200, 1489968000, 1489968000, 
1490572800, 1490572800), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), end_date = structure(c(1488758400, 1488758400, 1489363200, 
1489363200, 1489968000, 1489968000, 1490572800, 1490572800, 1491177600, 
1491177600), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    yyyy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = c("2017", "2018"), class = "factor"), mmm = c("Feb", 
    "Feb", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar"
    ), dd = c(27, 27, 6, 6, 13, 13, 20, 20, 27, 27), season = c("Spring", 
    "Spring", "Spring", "Spring", "Spring", "Spring", "Spring", 
    "Spring", "Spring", "Spring"), parameter = c("Cl", "Na", 
    "Cl", "Na", "Cl", "Na", "Cl", "Na", "Cl", "Na"), Gruvebadet = c(301.983272818835, 
    212.350184538299, 568.590207629836, 383.734431751691, 1052.32022102137, 
    678.971053100071, 224.264161262609, 165.3726548191, 429.556015888673, 
    410.051446157094), Nordpolhotellet = c(490, 330, 610, 570, 
    350, 250, 400, 460, 600, 490), Zeppelin = c(294.285714285714, 
    202.142857142857, 311.428571428571, 235.714285714286, 772, 
    474, 132.857142857143, 145.714285714286, 231.428571428571, 
    216.428571428571)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

I would like to draw linear regression with and without intercept zero on one scatter plot.我想在一个散点 plot 上绘制具有和不具有截距零的线性回归。 I use this code to do that:我使用这段代码来做到这一点:

my.formula <- y ~ x # linear equation without intercept zero
my.formula2 <- y ~ x - 1 #linear equation with intercept zero

library(ggplot2)
p <- ggplot(data=nacl2, aes(y=Nordpolhotellet, x=Gruvebadet)) + 
  geom_point() + geom_smooth(method="lm", formula=my.formula, se=F, col="red") +
  geom_point() + geom_smooth(method="lm", formula=my.formula2, se=F, col="blue") + 
  theme_bw()
p2 <- p + facet_wrap(~parameter, ncol=2, scales="free", 
                     labeller=as_labeller(c(Na="Na+", Cl="Cl-", NH4="NH4+", 
                                            K="K+", Mg="Mg++", Ca="Ca++", NO3="NO3-", 
                                            SO4="SO4--"))) +
  theme(strip.background=element_blank(), strip.placement="outside") +
  labs(y="Nordpolhotellet, Concentration (ng/m3)", x="Gruvebadet, Concentration (ng/m3)")

Then I want to add regression equation and R2 of each regression in one graph as well.然后我想在一张图中添加回归方程和每个回归的 R2。 I add this code:我添加此代码:

library("ggpmisc")
p3 <- p2 + stat_poly_eq(aes(label=paste(stat(eq.label), stat(rr.label), sep="*\", \"*")), 
                        formula=my.formula, coef.digits=4, rr.digits=3, parse=TRUE) + 
  stat_poly_eq(aes(label=paste(stat(eq.label), stat(rr.label), sep="*\", \"*")), 
               formula=my.formula2, coef.digits=4, rr.digits=3, parse=TRUE)
p3


However, I got the result where regression equation and R2 overlap with each other like this:但是,我得到了回归方程和 R2 相互重叠的结果,如下所示:

这个

I desire to put equation and R2 in different line for my case.对于我的情况,我希望将方程和 R2 放在不同的行中。 How to do that?怎么做?

Luckily, I found the solution.幸运的是,我找到了解决方案。 I need to adjust the position of second equation because it should be below first equation.我需要调整第二个方程的 position 因为它应该低于第一个方程。 I use label.x.npc and label.y.npc by trial and error to adjust the position.我通过反复试验使用 label.x.npc 和 label.y.npc 来调整 position。 Finally, found the best position I desire.最后,找到了我想要的最好的 position。 Here is the completed code:这是完成的代码:

my.formula <- y ~ x # linear equation without intercept zero
my.formula2 <- y ~ x - 1 #linear equation with intercept zero
library(ggplot2)
library(ggpmisc)

#Add two regression line with different formula into scatterplot
p<-ggplot(data=df3,aes(y=Nordpolhotellet,x=Gruvebadet))+geom_point()+
      geom_smooth(method="lm",formula=my.formula,se=F,col="red")+
      geom_smooth(method="lm",formula=my.formula2,se=F,col="blue")+theme_bw()

#Make different scatter plot based on parameter
p2<-p+facet_wrap(~parameter, ncol=2, scales="free", labeller=as_labeller(c(Na="Na+", Cl="Cl-"))) + 
     theme(strip.background=element_blank(), strip.placement="outside") + 
     labs(y="Nordpolhotellet, Concentration (ng/m3)", x="Gruvebadet, Concentration (ng/m3)")

#Add regression equation and R2 for each line into graph
p3<-p2+stat_poly_eq(aes(label =  paste(stat(eq.label),stat(rr.label), sep = "*\", \"*")),
          formula=my.formula,coef.digits = 4,rr.digits=3,parse=TRUE,col="red")+
    stat_poly_eq(aes(label =  paste(stat(eq.label),stat(rr.label), sep = "*\", \"*")),
          formula=my.formula2,coef.digits = 4,rr.digits=3,parse=TRUE,col="blue",
             label.x.npc = 0.05, label.y.npc = 0.88)

#Display final graph
p3

Here is scatter plot that I desire:这是我想要的分散 plot :

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM