5

I want to make a ggplot with a log regression and want to show the R2 and p-value. I tried stat_cor, but it only shows R2 and p-value for a linear regression. I tried to incorporate "formula=y~log(x)" into stat_cor, but sais unknown parameter: formula. Do I have to use a different function to make that happen?

ggplot(data = Data,aes(x=Carbon_per,y=Pyrite_per,col=Ecosystem,shape=Ecosystem)) +
  geom_smooth(method='lm', formula=y~log(x))+
  geom_point() +
  stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")))

Cheers, Gloria

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Does this answer your question? [Add regression line equation and R^2 on graph](https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph) – UseR10085 Jun 19 '20 at 05:11
  • 1
    No, I managed to get R2 and p valeu for a linear regression , but I need them for a log regression – Gloria Rooibos Jun 19 '20 at 05:33
  • You can log transform the x before plotting and then apply linear regression only (y~x). – UseR10085 Jun 19 '20 at 05:41
  • This does make sense, but I do not want to show a log(x) in my plot but the normal data with a log regression. I guess there is a way to combine the normal plot with a log(x) regression with a y~x stat_cor of the log(x) data. I will work on that. – Gloria Rooibos Jun 19 '20 at 06:17
  • Hi Bappa, I got it. Thanks for your help! – Gloria Rooibos Jun 19 '20 at 06:23

1 Answers1

2

Are you looking for something like this?

library(ggpubr)
library(ggplot2)
ggplot(data = mtcars, aes(x = log(wt), y = mpg)) +
                   geom_smooth(method = "lm", 
                               formula = y ~ x) +
                   geom_point() +
                   stat_cor(label.y = 40)+ 
                   stat_regline_equation(label.y = 45) 

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • Hi Zhiqiang, Thanks for your comment. This exactly what I have done, the problem is that the R2 and p value describe a linear regession not a log regression. I compared y~x and y~log(x) and it gives me the same R2 and p values, so I guess stat_cor can only do linear regressions.Or can I adjust that? – Gloria Rooibos Jun 19 '20 at 05:38
  • I have edited my answer. I will delete it if it is not what you wanted. Cheers – Zhiqiang Wang Jun 19 '20 at 05:44