[R] How to add "slope" in the scatter plot

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Feb 11 16:16:09 CET 2022


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 using 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.



More information about the R-help mailing list