[Damn, forgot default reply-to is to the last poster: sorry Rui.] That's taught me some fun stuff but I wonder if the first "formula = formula" is correct. It throws an error for me and I think the code works when I comment it out and it looks to me from ?geom_smooth that it is optional here (and defaults to NULL). Very best Rui and all, Chris ----- Original Message -----> From: "Rui Barradas" <ruipbarradas at sapo.pt> > To: "Marna Wagley" <marna.wagley at gmail.com>, "r-help mailing list" <r-help at r-project.org> > Sent: Friday, 11 February, 2022 16:16:09 > Subject: Re: [R] How to add "slope" in the scatter plot> Hello, > > If instead of ..coef.. you use ..eq.label.. you'll have the equation. > > > library(ggplot2) > library(ggpmisc) > > ggplot(daT, aes(x= x, y=y, group=1)) + > geom_point(size=0, alpha=0.3, colour="pink") + > geom_smooth( > method = "lm", > formula = formula, se = TRUE, > linetype="dashed", > size = 2) + > stat_poly_eq( > aes(label = paste(..eq.label.., ..rr.label.., ..p.value.label.., > sep = "*`,`~~")), > formula = y ~ x, > parse = TRUE, > label.x.npc = "right", > vstep = 0.05, > size = 5) > > > Hope this helps, > > Rui Barradas > > > > ?s 05:25 de 11/02/2022, Marna Wagley escreveu: >> Hi R users, >> I was trying to add slope, R2 and p value in the scatter plot, but I was >> able to add only r2 and p value but not slope in the example data using the >> following code. Would you mind checking the code? >> >> daT<-structure(list(x = c(59.9, 96.4, 91.2, 51.4, 11.8, 38.3, 65, >> 38.3, 35.6, 48.2, 15, 24.6, 60.9), y = c(77.9, 51.8, 53.8, 1.8, >> 84.7, 49.6, 21.2, 39.5, 22.7, 71, 16.5, 85.8, 10.7)), >> row.names = c(NA,-13L), class = "data.frame") >> >> formula <- y ~ poly(x, 1, raw = TRUE) >> ggplot(daT, aes(x= x, y=y, group=1))+ >> geom_point(size=0, alpha=0.3, colour="pink")+geom_smooth(method = >> "lm",linetype="dashed", >> formula = formula, se = T, size = 2)+stat_poly_eq(formula = y ~ x, >> aes(label = paste(..coef.., ..rr.label..,..p.value.label.., sep = "*`,`~")), >> parse = TRUE,label.x.npc = "right", vstep = 0.05, size = 5) >> >> thanks, >> MW >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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.-- Chris Evans (he/him) <chris at psyctc.org> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, University of Roehampton, London, UK. Work web site: https://www.psyctc.org/psyctc/ CORE site: https://www.coresystemtrust.org.uk/ Personal site: https://www.psyctc.org/pelerinage2016/ OMbook: https://ombook.psyctc.org/book/ [[alternative HTML version deleted]]
Chris Evans
2022-Feb-13 09:07 UTC
[R] [Impersonated] Re: How to add "slope" in the scatter plot
My bad: I had not noticed that the line: formula <- y ~ poly(x, 1, raw = TRUE) that was in the OP post was not in Rui's reply. As ever, I should concentrate harder! Apologies for cruft on cruft but thought I should put this in the archived thread. Very best all, Chris ----- Original Message -----> From: "Chris Evans" <chris at psyctc.org> > To: R-help at r-project.org > Sent: Friday, 11 February, 2022 18:46:32 > Subject: [Impersonated] Re: [R] How to add "slope" in the scatter plot> [Damn, forgot default reply-to is to the last poster: sorry Rui.] > > That's taught me some fun stuff but I wonder if the first "formula = formula" is > correct. > It throws an error for me and I think the code works when I comment it out and > it looks > to me from ?geom_smooth that it is optional here (and defaults to NULL). > > Very best Rui and all, > > Chris > > ----- Original Message ----- >> From: "Rui Barradas" <ruipbarradas at sapo.pt> >> To: "Marna Wagley" <marna.wagley at gmail.com>, "r-help mailing list" >> <r-help at r-project.org> >> Sent: Friday, 11 February, 2022 16:16:09 >> Subject: Re: [R] How to add "slope" in the scatter plot > >> Hello, >> >> If instead of ..coef.. you use ..eq.label.. you'll have the equation. >> >> >> library(ggplot2) >> library(ggpmisc) >> >> ggplot(daT, aes(x= x, y=y, group=1)) + >> geom_point(size=0, alpha=0.3, colour="pink") + >> geom_smooth( >> method = "lm", >> formula = formula, se = TRUE, >> linetype="dashed", >> size = 2) + >> stat_poly_eq( >> aes(label = paste(..eq.label.., ..rr.label.., ..p.value.label.., >> sep = "*`,`~~")), >> formula = y ~ x, >> parse = TRUE, >> label.x.npc = "right", >> vstep = 0.05, >> size = 5) >> >> >> Hope this helps, >> >> Rui Barradas >> >> >> >> ?s 05:25 de 11/02/2022, Marna Wagley escreveu: >>> Hi R users, >>> I was trying to add slope, R2 and p value in the scatter plot, but I was >>> able to add only r2 and p value but not slope in the example data using the >>> following code. Would you mind checking the code? >>> >>> daT<-structure(list(x = c(59.9, 96.4, 91.2, 51.4, 11.8, 38.3, 65, >>> 38.3, 35.6, 48.2, 15, 24.6, 60.9), y = c(77.9, 51.8, 53.8, 1.8, >>> 84.7, 49.6, 21.2, 39.5, 22.7, 71, 16.5, 85.8, 10.7)), >>> row.names = c(NA,-13L), class = "data.frame") >>> >>> formula <- y ~ poly(x, 1, raw = TRUE) >>> ggplot(daT, aes(x= x, y=y, group=1))+ >>> geom_point(size=0, alpha=0.3, colour="pink")+geom_smooth(method >>> "lm",linetype="dashed", >>> formula = formula, se = T, size = 2)+stat_poly_eq(formula = y ~ x, >>> aes(label = paste(..coef.., ..rr.label..,..p.value.label.., sep = "*`,`~")), >>> parse = TRUE,label.x.npc = "right", vstep = 0.05, size = 5) >>> >>> thanks, >>> MW >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> 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.-- Chris Evans (he/him) <chris at psyctc.org> Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, University of Roehampton, London, UK. Work web site: https://www.psyctc.org/psyctc/ CORE site: https://www.coresystemtrust.org.uk/ Personal site: https://www.psyctc.org/pelerinage2016/ OMbook: https://ombook.psyctc.org/book/