[英]loop to plot several fitted curves in ggplot?
在這個數據集中,我有一個因子,該因子具有3個水平(iso)和兩個連續變量(temp和diam)
library(ggplot2)
library(nlme)
zz <-(" iso temp diam
Itiquira 22 5.0
Itiquira 22 4.7
Itiquira 22 5.4
Itiquira 25 5.8
Itiquira 25 5.4
Itiquira 25 5.0
Itiquira 28 4.9
Itiquira 28 5.2
Itiquira 28 5.2
Itiquira 31 4.2
Itiquira 31 4.0
Itiquira 31 4.1
Londrina 22 4.5
Londrina 22 5.0
Londrina 22 4.4
Londrina 25 5.0
Londrina 25 5.5
Londrina 25 5.3
Londrina 28 4.6
Londrina 28 4.3
Londrina 28 4.9
Londrina 31 4.4
Londrina 31 4.1
Londrina 31 4.4
Sinop 22 4.5
Sinop 22 5.2
Sinop 22 4.6
Sinop 25 5.7
Sinop 25 5.9
Sinop 25 5.8
Sinop 28 6.0
Sinop 28 5.5
Sinop 28 5.8
Sinop 31 4.5
Sinop 31 4.6
Sinop 31 4.3"
)
df <- read.table(text=zz, header = TRUE)
我為每個因子水平擬合一條曲線,並且需要將它們繪制在同一圖形中。
有沒有一種方法可以立即繪制所有曲線,從而避免對每個水平因子(iso)重復相同的函數“ + geom_smooth(...)”?
daf <- groupedData(diam ~ temp | iso, data = df, order = FALSE)
ip <- ggplot(data=daf, aes(x=temp, y=diam, colour = iso)) +
geom_point() + facet_wrap(~iso)
ip + geom_smooth(method = "nls",
method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3),
start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
se = F, size = 0.5, data = subset(daf, iso=="Itiquira")) +
geom_smooth(method = "nls",
method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3),
start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
se = F, size = 0.5, data = subset(daf, iso=="Londrina")) +
geom_smooth(method = "nls",
method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3),
start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
se = F, size = 0.5, data = subset(daf, iso=="Sinop"))
您可以獲得相同的圖,而無需為每個水平因子(iso)重復相同的功能,如下所示:
ggplot(data=daf, aes(x=temp, y=diam, colour = iso)) +
geom_point() +
facet_wrap(~iso) +
geom_smooth(method="nls",
method.args=list(formula=y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3),
start=list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
se = F,
size = 0.5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.