This is example of data and graph
##generated date
GN<- c(1,2,3,4,5,6,7,8,9,10,11,12,13)
Yield<- c(10,30,50,65,75,80,90,100,105,110,115,115,116)
dataA<- data.frame(GN,Yield)
##ggplot
ggplot(data=dataA, aes(x=GN, y=Yield))+
geom_smooth(method="lm", formula=y~poly(x,2, raw=TRUE),
level=0.95, se=FALSE, linetype=1, size=1, color="Red") +
geom_point(col="Black", size=4) +
scale_x_continuous(breaks = seq(0,15,3), limits = c(0,15)) +
scale_y_continuous(breaks = seq(0,120,20), limits = c(0,120)) +
labs(x="Grain number", y="Yield") +
theme_grey(base_size=17, base_family="serif")+
theme(axis.line= element_line(size=0.5, colour="black"))+
windows(width=5.5, height=5)
and I made quadratic graph using ggplot. Now I want to draw a quadratic plateau graph. So I studied the model like below.
x<-dataA$GN
y<-dataA$Yield
Mean <- function(x, alpha, beta, gamma) {pmin(alpha + beta*x + gamma*x*x, alpha - beta^2/(4 * gamma))}
fm <- nls(y ~ Mean(x, alpha, beta, gamma), start = list(alpha = 0.45, beta = 0.05, gamma = -0.0025))
fm
summary(fm)
###
Formula: y ~ Mean(x, alpha, beta, gamma)
Parameters:
Estimate Std. Error t value Pr(>|t|)
alpha -5.04895 2.77042 -1.822 0.0984 .
beta 19.47453 0.91014 21.397 1.11e-09 ***
gamma -0.78821 0.06326 -12.460 2.05e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.83 on 10 degrees of freedom
Number of iterations to convergence: 1
Achieved convergence tolerance: 7.61e-07
Then, how can I draw a quadratic plateau in ggplot? Simply I thought like below.
ggplot(data=dataA, aes(x=GN, y=Yield))+
geom_smooth(method="nls", formula=fm, level=0.95, se=FALSE,
linetype=1, size=1, color="Red") +
geom_point(col="Black", size=4) +
scale_x_continuous(breaks = seq(0,15,3), limits = c(0,15)) +
scale_y_continuous(breaks = seq(0,120,20), limits = c(0,120)) +
labs(x="Grain number", y="Yield") +
theme_grey(base_size=17, base_family="serif")+
theme(axis.line= element_line(size=0.5, colour="black"))+
windows(width=5.5, height=5)
But warning message pops up and there is no line.
Warning message:
Computation failed in `stat_smooth()`
Caused by error in `getInitial.default()`:
! no 'getInitial' method found for "function" objects
Could you tell me how to draw a quadratic plateau graph in ggplot with the data? If you introduce how to make it with this simple data, I believe I can make the graph with my actual data.
Always many thanks!!