R ggplot2 exponential regression with R² and p

2019-09-13 07:59发布

I am trying to do a exponential regression in ggplot2. So first my skript:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
 geom_point(shape=1) +  stat_smooth(method = 'nls', formula = y~a*exp(b*x), aes(colour = 'Exponential'), se = FALSE)
g <- g + theme_classic()
g <- g + theme(panel.grid.major=element_blank())
g <- g + theme(panel.grid.minor=element_blank())
g <- g + theme(axis.line.x=element_line(color="black"),
           axis.line.y=element_line(color="black"),
           panel.border=element_blank(),
           panel.background=element_blank())
g <- g + labs(x="\ndatax",y="datay\n")
g <- g + theme(axis.text.y=element_text(size=14))
g <- g + theme(axis.text.x=element_text(size=14))
g <- g + theme(axis.title.y=element_text(size=18,vjust=1))
g <- g + theme(axis.title.x=element_text(size=18,vjust=1))
g

This is the image that I got

enter image description here

As a R-beginner I did the script by mixing scripts of mine and the internet. I always get the following error:

"In (function (formula, data = parent.frame(), start, control = nls.control(), : No starting values specified for some parameters. Initializing ‘a’, ‘b’ to '1.'.Consider specifying 'start' or using a selfStart model"

I did not found a better way to do the exponential graph, yet.

In addition, I would like to change the color of the graph into black and delete the legend and I would like to have the R² and p value in the graph. (maybe as well the confidence intervals?)

标签: r ggplot2 nls
1条回答
爷、活的狠高调
2楼-- · 2019-09-13 08:19

It's not easy to answer without a reproducible example and so many questions. Are you sure the message you reported is an error and not a warning instead? On my own laptop, with dataset 'iris', I got a warning...

However, how you can read on ?nls page on R documentation, you should provide through the parameter "start" an initial value for starting the estimates to help finding the convergence. If you don't provide it, nls() itself should use some dummy default values (in your case, a and b are set to 1).

You could try something like this:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
     geom_point(shape=1) +  stat_smooth(method = 'nls', 
     method.args = list(start = c(a=1, b=1)), 
     formula = y~a*exp(b*x), colour = 'black', se = FALSE)

You told R that the colour of the plot is "Exponential", I think that so is going to work (I tried with R-base dataset 'iris' and worked). You can notice that I passed the start parameter as an element of a list passed to 'method.args': this is a new feature in ggplot v2.0.0.

Hope this helps

Edit: just for completeness, I attach the code I reproduced on my laptop with default dataset: (please take care that it has no sense an exponential fit with such a dataset, but the code runs without warning)

library(ggplot2)
data('iris')
g1 <- ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
      geom_point(color='green') +geom_smooth(method = 'nls',
      method.args = list(start=c(a=1, b=1)), se = FALSE, 
      formula = y~a*exp(b*x), colour='black')
g1
查看更多
登录 后发表回答