Gloria Rooibos
Gloria Rooibos

Reputation: 51

Show R2 and p-value in ggplot for y~log(x) fuction

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

Upvotes: 5

Views: 6394

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

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

Upvotes: 2

Related Questions