Potential trend line using ggplot

2

These are my data

 Qmodelo <- c(10,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500)
 Hmodelo <- c(73.57,111.39,152.31,181.50,204.73,225.42,243.94,259.77,273.95,287.90,299.20,309.62,319.15,328.17, 336.72, 344.84, 352.63, 360.11, 367.34, 374.31, 382.25)
 df2 <- data.frame(Qmodelo,Hmodelo)

Perform a script to calculate the potential relationship and plot the equation

power_eqn = function(df2, start = list(a =1,b=1)){
m = nls(Qmodelo ~ a*Hmodelo^b, start = start, data = df2);
eq <- substitute(italic(y) == a  ~italic(x)^b, 
               list(a = format(coef(m)[1], digits = 2), 
                    b = format(coef(m)[2], digits = 2)))
as.character(as.expression(eq));                 
}

lm_r <- function(df2){
m <- lm(Qmodelo ~ Hmodelo, df2);
eq2 <- substitute(italic(r)^2~"="~r2,list(r2=format(summary(m)$r.squared,digits = 4)))
as.character(as.expression(eq2)); 
}

RPotencial <- ggplot(df2,aes(x=Hmodelo,y=Qmodelo)) +
geom_point() + 
stat_smooth(method = 'nls', formula = Qmodelo ~ a*Hmodelo^b, se = FALSE, start = list(a =1,b=1)) +
geom_text(x = 130, y = 400, label = power_eqn(df2), parse = TRUE)+
geom_text(x = 140, y = 350, label = lm_r(df2), parse = TRUE)+
ylab(bquote('Gasto liquido ('*m^3~s^-1*')'))+
xlab(bquote("Altura (cm)"))+
ggtitle("Curva de gasto liquido Est. La Coja")

RPotencial

But this error is displayed

Error: Unknown parameters: start

I guess it may be due to the range of coefficients that I have chosen. My question is whether it is possible to know that range automatically.

    
asked by AnesG 23.08.2016 в 20:48
source

1 answer

1

Since ggplot version> = 2.0 it seems that the start parameter for the nls method must be passed in the following way:

# ...
stat_smooth(method = 'nls', formula = Qmodelo ~ a*Hmodelo^b, se = FALSE, method.args=list(start=c(a=1,b=1))) +
# ....

But now it will give an error for exceeding the number of iterations. You should adjust the parameters a and b a bit more.

    
answered by 26.08.2016 в 15:59