0

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)

enter image description here

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)

enter image description here

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!!

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15
  • Try [tag:ggpmisc] as given [here](https://stackoverflow.com/a/73588217/707145). – MYaseen208 Dec 13 '22 at 02:55
  • 1
    Part of the problem is likely that you’re passing `fm` to `formula` inside `geom_smooth()`, but `fm` is a model, not a formula. Try changing the `formula` arg to something like `y ~ Mean(x, alpha, beta, gamma)`. You’ll also need to pass your `start` list using `method.args`. – zephryl Dec 13 '22 at 03:20

1 Answers1

1

You’re passing fm to formula inside geom_smooth(), but fm is a fitted model, not a formula. Change the formula arg to y ~ Mean(x, alpha, beta, gamma), and pass your start list using method.args.

library(ggplot2)

Mean <- function(x, alpha, beta, gamma) {pmin(alpha + beta*x + gamma*x*x, alpha - beta^2/(4 * gamma))}

ggplot(data=dataA, aes(x=GN, y=Yield))+
  geom_smooth(method="nls", formula= y ~ Mean(x, alpha, beta, gamma), method.args = list(start = list(alpha = 0.45, beta = 0.05, gamma = -0.0025)), 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"))

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • Thank you so much!! By the way, the curve was not plateau, I tried this code to another data, and it is the same as quadratic curve. I want to show the plateau line at certain point. Do you know how to do it? – Jin.w.Kim Dec 13 '22 at 06:56