Rui Barradas
2023-Dec-10 22:50 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
?s 22:35 de 10/12/2023, varin sacha via R-help escreveu:> > Dear R-experts, > > Here below my R code, as my X-axis is "year", I must be missing one or more steps! I am trying to get the regression line with the 95% confidence bands around the regression line. Any help would be appreciated. > > Best, > S. > > > ############################################# > library(ggplot2) > > df=data.frame(year=factor(c("2012","2015","2018","2022")), score=c(495,493, 495, 474)) > > ggplot(df, aes(x=year, y=score)) + geom_point( ) + geom_smooth(method="lm", formula = score ~ factor(year), data = df) + labs(title="Standard linear regression for France", y="PISA score in mathematics") + ylim(470, 500) > ############################################# > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, I don't see a reason why year should be a factor and the formula in geom_smooth is wrong, it should be y ~ x, the aesthetics envolved. It still doesn't plot the CI's though. There's a warning and I am not understanding where it comes from. But the regression line is plotted. ggplot(df, aes(x = as.numeric(year), y = score)) + geom_point() + geom_smooth(method = "lm", formula = y ~ x) + labs( title = "Standard linear regression for France", x = "Year", y = "PISA score in mathematics" ) + ylim(470, 500) #> Warning message: #> In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Bert Gunter
2023-Dec-10 23:52 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
This can easily be done using predict.lm to get the intervals (confidence or prediction). ?predict.lm contains a plotting example using ?matplot from the graphics package. Here's a somewhat verbose version for your example (first converting Year to numeric, of course): df=data.frame(year= c(2012,2015,2018,2022), score=c(495,493, 495, 474)) fitted <- lm(score ~ year, data = df) with(df, matplot(x = year, y = cbind(score,predict(fitted,interval = 'conf', level = .95)) ,type = c('p', rep('l',3)) ,pch = 16 ,lty = c('blank','solid', 'dashed','dashed') ## or use numeric values of 0,1,2,2 ,lwd = c(0,2,1,1) ,col = c('black','darkblue', 'red','red') ,xlab = 'Year' ,ylab = 'Data with Fitted Line and Conf Intervals' ) ) Cheers, Bert On Sun, Dec 10, 2023 at 2:51?PM Rui Barradas <ruipbarradas at sapo.pt> wrote:> ?s 22:35 de 10/12/2023, varin sacha via R-help escreveu: > > > > Dear R-experts, > > > > Here below my R code, as my X-axis is "year", I must be missing one or > more steps! I am trying to get the regression line with the 95% confidence > bands around the regression line. Any help would be appreciated. > > > > Best, > > S. > > > > > > ############################################# > > library(ggplot2) > > > > df=data.frame(year=factor(c("2012","2015","2018","2022")), > score=c(495,493, 495, 474)) > > > > ggplot(df, aes(x=year, y=score)) + geom_point( ) + > geom_smooth(method="lm", formula = score ~ factor(year), data = df) + > labs(title="Standard linear regression for France", y="PISA score in > mathematics") + ylim(470, 500) > > ############################################# > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > Hello, > > I don't see a reason why year should be a factor and the formula in > geom_smooth is wrong, it should be y ~ x, the aesthetics envolved. > It still doesn't plot the CI's though. There's a warning and I am not > understanding where it comes from. But the regression line is plotted. > > > > ggplot(df, aes(x = as.numeric(year), y = score)) + > geom_point() + > geom_smooth(method = "lm", formula = y ~ x) + > labs( > title = "Standard linear regression for France", > x = "Year", > y = "PISA score in mathematics" > ) + > ylim(470, 500) > #> Warning message: > #> In max(ids, na.rm = TRUE) : no non-missing arguments to max; > returning -Inf > > > > Hope this helps, > > Rui Barradas > > > > -- > Este e-mail foi analisado pelo software antiv?rus AVG para verificar a > presen?a de v?rus. > www.avg.com > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Daniel Nordlund
2023-Dec-11 22:27 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
On 12/10/2023 2:50 PM, Rui Barradas wrote:> ?s 22:35 de 10/12/2023, varin sacha via R-help escreveu: >> >> Dear R-experts, >> >> Here below my R code, as my X-axis is "year", I must be missing one >> or more steps! I am trying to get the regression line with the 95% >> confidence bands around the regression line. Any help would be >> appreciated. >> >> Best, >> S. >> >> >> ############################################# >> library(ggplot2) >> ? df=data.frame(year=factor(c("2012","2015","2018","2022")), >> score=c(495,493, 495, 474)) >> ? ggplot(df, aes(x=year, y=score)) + geom_point( ) + >> geom_smooth(method="lm", formula = score ~ factor(year), data = df) + >> labs(title="Standard linear regression for France", y="PISA score in >> mathematics") + ylim(470, 500) >> ############################################# >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > Hello, > > I don't see a reason why year should be a factor and the formula in > geom_smooth is wrong, it should be y ~ x, the aesthetics envolved. > It still doesn't plot the CI's though. There's a warning and I am not > understanding where it comes from. But the regression line is plotted. > > > > ggplot(df, aes(x = as.numeric(year), y = score)) + > ? geom_point() + > ? geom_smooth(method = "lm", formula = y ~ x) + > ? labs( > ??? title = "Standard linear regression for France", > ??? x = "Year", > ??? y = "PISA score in mathematics" > ? ) + > ? ylim(470, 500) > #> Warning message: > #> In max(ids, na.rm = TRUE) : no non-missing arguments to max; > returning -Inf > > > > Hope this helps, > > Rui Barradas > > >After playing with this for a little while, I realized that the problem with plotting the confidence limits is the addition of ylim(470, 500).? The confidence values are outside the ylim values.? Remove the limits, or increase the range, and the confidence curves will plot. Hope this is helpful, Dan -- Daniel Nordlund Port Townsend, WA USA -- This email has been checked for viruses by Avast antivirus software. www.avast.com