non linear regression (power law using log transformation)

Hello everyone,
I want to fit power-law model through log transformation. I tried with r base plot. However, now I need it by ggplot2. Can anybody help me?
VPD: independent variable ranges from 0 to 1.5 like 0.4,0.32 and so on
X1: ranges from 50 to 300 like 55,170,225 and so on
Model1=lm(log(Merged$X1)~log(Merged$VPD))
summary(Model1)
plot(Merged$X1~Merged$VPD)
xv<-seq(0.15,1.1,0.1)
yv<-exp(5.67775)*xv^0.36681 # model1 fit: intercept&slope from Model1 (Y=aX^b)
lines(xv,yv,col=2,lwd=1,lty=1)
text(0.75,155,expression("Upland, p<0.001," ~ R^2~ "=0.69"),cex=0.8)

See the FAQ: How to do a minimal reproducible example reprex for beginners for the preferred way to ask coding questions.

suppressPackageStartupMessages({
  library(ggplot2)
})

# generate fake data

set.seed(137)
VPD <- sample(seq(0, 1.5, 0.01), 500, replace = TRUE)
X1 <- sample(seq(50, 300, 5), 500, replace = TRUE)
Merged <- data.frame(VPD = VPD, X1 = X1)

# data from post

xv <- seq(0.15, 1.1, 0.1)
yv <- exp(5.67775) * xv^0.36681

obj <- data.frame(xv = xv, yv = yv)

# base graphics

plot(X1 ~ VPD)
lines(xv, yv, col = 2, lwd = 1, lty = 1)

# ggplot

p <- ggplot(Merged,aes(VPD,X1)) + 
  geom_point() +
  geom_line(obj,mapping = aes(xv,yv), color = "red", size = 1.5) + 
  theme_minimal()

p

Thank you. It works now.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.