[英]How to draw a quadratic plateau in ggplot?
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.我使用 ggplot 制作了二次图。 Now I want to draw a quadratic plateau graph.
现在我想画一个二次高原图。 So I studied the model like below.
所以我研究了如下所示的 model。
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?那么,如何在 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?你能告诉我如何用数据在 ggplot 中绘制二次高原图吗? 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!!总是非常感谢!
You're passing fm
to formula
inside geom_smooth()
, but fm
is a fitted model, not a formula.您将
fm
传递给geom_smooth()
内的formula
,但fm
是一个拟合 model,而不是公式。 Change the formula
arg to y ~ Mean(x, alpha, beta, gamma)
, and pass your start
list using method.args
.将
formula
arg 更改为y ~ Mean(x, alpha, beta, gamma)
,并使用method.args
传递您的start
列表。
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.