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. 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. 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:
I desire to put equation and R2 in different line for my case. 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. I use label.x.npc and label.y.npc by trial and error to adjust the position. Finally, found the best position I desire. 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.