varin sacha
2023-Dec-12 21:19 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
Dear Ben, Dear Daniel, Dear Rui, Dear Bert, Here below my R code. I really appreciate all your comments. My R code is perfectly working but there is still something I would like to improve. The X-axis is showing ? 2012.5 ; ? 2015.0 ? ; ? 2017.5 ? ; ?2020.0 I would like to see on X-axis only the year (2012 ; 2015 ; 2017 ; 2020). How to do?? ######### library(ggplot2) ? df=data.frame(year= 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 = y ~ x) + ?labs(title = "Standard linear regression for France", x = "Year", y = "PISA score in mathematics") + scale_y_continuous(limits=c(470,500),oob=scales::squish) ######### Le lundi 11 d?cembre 2023 ? 23:38:06 UTC+1, Ben Bolker <bbolker at gmail.com> a ?crit : On 2023-12-11 5:27 p.m., Daniel Nordlund wrote:> 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 >? Or use + scale_y_continuous(limits = c(470, 500), oob = scales::squish) ______________________________________________ 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.
Ben Bolker
2023-Dec-12 22:14 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
Use scale_x_continuous() and specify your desired breaks On Tue, Dec 12, 2023, 4:19 PM varin sacha <varinsacha at yahoo.fr> wrote:> Dear Ben, > Dear Daniel, > Dear Rui, > Dear Bert, > > Here below my R code. > I really appreciate all your comments. My R code is perfectly working but > there is still something I would like to improve. The X-axis is showing > 2012.5 ; 2015.0 ; 2017.5 ; 2020.0 > I would like to see on X-axis only the year (2012 ; 2015 ; 2017 ; 2020). > How to do? > > > ######### > library(ggplot2) > > df=data.frame(year= 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 = y ~ x) + > labs(title = "Standard linear regression for France", x = "Year", y > "PISA score in mathematics") + > scale_y_continuous(limits=c(470,500),oob=scales::squish) > ######### > > > > > > > > > > Le lundi 11 d?cembre 2023 ? 23:38:06 UTC+1, Ben Bolker <bbolker at gmail.com> > a ?crit : > > > > > > > > On 2023-12-11 5:27 p.m., Daniel Nordlund wrote: > > 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 > > > > Or use + scale_y_continuous(limits = c(470, 500), oob = scales::squish) > > > ______________________________________________ > 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]]
Robert Baer
2023-Dec-13 00:36 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
coord_cartesian also seems to work for y, and including the breaks = .? How about: df=data.frame(year= 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 = y ~ x) + ? labs(title = "Standard linear regression for France", x = "Year", y = "PISA score in mathematics") + ? coord_cartesian(ylim=c(470,500)) + ? scale_x_continuous(breaks = 2012:2022) On 12/12/2023 3:19 PM, varin sacha via R-help wrote:> Dear Ben, > Dear Daniel, > Dear Rui, > Dear Bert, > > Here below my R code. > I really appreciate all your comments. My R code is perfectly working but there is still something I would like to improve. The X-axis is showing ? 2012.5 ; ? 2015.0 ? ; ? 2017.5 ? ; ?2020.0 > I would like to see on X-axis only the year (2012 ; 2015 ; 2017 ; 2020). How to do? > > > ######### > library(ggplot2) > > df=data.frame(year= 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 = y ~ x) + > ?labs(title = "Standard linear regression for France", x = "Year", y = "PISA score in mathematics") + scale_y_continuous(limits=c(470,500),oob=scales::squish) > ######### > > > > > > > > > > Le lundi 11 d?cembre 2023 ? 23:38:06 UTC+1, Ben Bolker <bbolker at gmail.com> a ?crit : > > > > > > > > On 2023-12-11 5:27 p.m., Daniel Nordlund wrote: >> 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 >> > ? Or use + scale_y_continuous(limits = c(470, 500), oob = scales::squish) > > > ______________________________________________ > 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. > > ______________________________________________ > 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.
Ebert,Timothy Aaron
2023-Dec-13 02:43 UTC
[R] ggplot2: Get the regression line with 95% confidence bands
Change year to a factor. Doing it in ggplot will not change the original data. ggplot(df, aes(x = as.factor(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") + scale_y_continuous(limits=c(470,500),oob=scales::squish) Regards, Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of varin sacha via R-help Sent: Tuesday, December 12, 2023 4:19 PM To: r-help at r-project.org; Ben Bolker <bbolker at gmail.com> Subject: Re: [R] ggplot2: Get the regression line with 95% confidence bands [External Email] Dear Ben, Dear Daniel, Dear Rui, Dear Bert, Here below my R code. I really appreciate all your comments. My R code is perfectly working but there is still something I would like to improve. The X-axis is showing 2012.5 ; 2015.0 ; 2017.5 ; 2020.0 I would like to see on X-axis only the year (2012 ; 2015 ; 2017 ; 2020). How to do? ######### library(ggplot2) df=data.frame(year= 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 = y ~ x) + labs(title = "Standard linear regression for France", x = "Year", y = "PISA score in mathematics") + scale_y_continuous(limits=c(470,500),oob=scales::squish) ######### Le lundi 11 d?cembre 2023 ? 23:38:06 UTC+1, Ben Bolker <bbolker at gmail.com> a ?crit : On 2023-12-11 5:27 p.m., Daniel Nordlund wrote:> 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://st/ >>> at.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl >>> .edu%7C104a304ff93043a854a408dbfb5809c1%7C0d4da0f84a314d76ace60a6233 >>> 1e1b84%7C0%7C0%7C638380127776926039%7CUnknown%7CTWFpbGZsb3d8eyJWIjoi >>> MC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C >>> %7C%7C&sdata=vDkrWWPIys%2FfrA00nTpEHWiYps3U6L6g4ACFkRs%2Fcmw%3D&rese >>> rved=0 >>> PLEASE do read the posting guide >>> http://www/ >>> .r-project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu% >>> 7C104a304ff93043a854a408dbfb5809c1%7C0d4da0f84a314d76ace60a62331e1b8 >>> 4%7C0%7C0%7C638380127776926039%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wL >>> jAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7 >>> C&sdata=hcvic6lEhrl4XpgEIctV4zhjz6ZgI9nWAHF4vLUbJyc%3D&reserved=0 >>> 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 >Or use + scale_y_continuous(limits = c(470, 500), oob = scales::squish) ______________________________________________ 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. ______________________________________________ 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.