简体   繁体   中英

Plotting power function line in a double logarithmic axis plot R

I am having a problem plotting a power function line to a plot that has both axis on log scale. I need to keep the axis on the log scale. I don't want to log transform my data. That's why I am using the log="xy" argument on the plot function as indicated below.

However, I haven't been able to add the power function to this plot. I've tried different ways so far, including the nls (below), but nothing seems to work.

Could anyone help me please? Thank you, Maíra

nls4=nls(Slope~i*CA^-z,start=list(i=0.1,z=-0.04),data=slpca1)
abline(nls4)
plot(Slope~CA,data=slpca1,xlab="Contributing area (ha)",ylab="Slope (m m[-1])",log="xy",
     xlim=c(0.001,1),ylim=c(0.01,1),xaxt="n",yaxt="n",cex.lab=2,cex.axis=1.5,
     cex=2,pch=16,family="serif") 
axis(side=1, at=(c(0.001,0.01,0.1,1)),labels=c(0.001,0.01,0.1,1)) 
axis(side=2,at=(c(0.01,0.1,1)),labels=c(0.01,0.1,1))

The best way to get help here is to provide a completely reproducible example (you gave us your code, but you didn't provide the data for slpca1 ) and keep that example as simple as possible (no need for axis labels, fonts, etc. unless it's an elemental part of the question).

So, I created some fake data, fit your nonlinear regression, and plotted the resulting fit. Hopefully this will help you get started, and you can make modifications as you wish.

# create some fake data so I can run your code
CA <- runif(20, 1, 20)
Slope <- 0.1*CA^-0.04 + rnorm(20, 0, 0.001)
slpca1 <- data.frame(CA, Slope)
rm(CA, Slope)

# fit a nonlinear regression
nls4 = nls(Slope ~ i*CA^-z, start=list(i=0.1,z=-0.04), data=slpca1)
# plot the observed values
plot(Slope~CA, data=slpca1, log="xy")
# plot the predicted line
ord <- order(slpca1$CA)
lines(slpca1$CA[ord], predict(nls4)[ord])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM