如何使用 facet_grid 在 ggplot2 中显示不同次数的多项式拟合?
How can I show different degree polynomial fits in ggplot2 with facet_grid?
我想使用小平面(因为我喜欢他们寻找它的方式)来显示递增阶数的多项式拟合。很容易将它们分别绘制如下:
df <- data.frame(x=rep(1:10,each=10),y=rnorm(100))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,2))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,3))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,4))
我知道我总是可以使用 grobs 以某种方式组合它们,但如果可能的话我想使用 facet_grid
组合它们。可能类似于:
poly2 <- df
poly2$degree <- 2
poly3 <- df
poly3$degree <- 3
poly4 <- df
poly4$degree <- 4
polyn <- rbind(poly2,poly3,poly4)
ggplot(polyn,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,degree)) +
facet_grid(degree~.)
这当然行不通,因为分面在 y~poly(x,degree)
上不起作用,因此 degree
从数据中提取。有什么办法可以做到这一点吗?
您始终可以手动预测点,然后很容易地进行分面,
## Data
set.seed(0)
df <- data.frame(x=rep(1:10,each=10),y=rnorm(100))
## Get poly fits
dat <- do.call(rbind, lapply(1:4, function(d)
data.frame(x=(x=runif(1000,0,10)),
y=predict(lm(y ~ poly(x, d), data=df), newdata=data.frame(x=x)),
degree=d)))
ggplot(dat, aes(x, y)) +
geom_point(data=df, aes(x, y), alpha=0.3) +
geom_line(color="steelblue", lwd=1.1) +
facet_grid(~ degree)
要添加置信区间,您可以将选项 interval='confidence'
与 predict
结合使用。您可能还对函数 ggplot2::fortify
感兴趣,以获得更多拟合统计数据。
dat <- do.call(rbind, lapply(1:4, function(d) {
x <- seq(0, 10, len=100)
preds <- predict(lm(y ~ poly(x, d), data=df), newdata=data.frame(x=x), interval="confidence")
data.frame(cbind(preds, x=x, degree=d))
}))
ggplot(dat, aes(x, fit)) +
geom_point(data=df, aes(x, y), alpha=0.3) +
geom_line(color="steelblue", lwd=1.1) +
geom_ribbon(aes(x=x, ymin=lwr, ymax=upr), alpha=0.3) +
facet_grid(~ degree)
我有一个非常丑陋的解决方案,其中 de plot 是多面的,并且为适当的数据子集绘制了拟合:
p1 <- ggplot(polyn,aes(x=x,y=y)) + facet_grid(.~degree)
p1 +
stat_smooth(data=polyn[polyn$degree==2,],formula=y~poly(x,2),method="lm") +
stat_smooth(data=polyn[polyn$degree==3,],formula=y~poly(x,3),method="lm") +
stat_smooth(data=polyn[polyn$degree==4,],formula=y~poly(x,4),method="lm")
产量
我想使用小平面(因为我喜欢他们寻找它的方式)来显示递增阶数的多项式拟合。很容易将它们分别绘制如下:
df <- data.frame(x=rep(1:10,each=10),y=rnorm(100))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,2))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,3))
ggplot(df,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,4))
我知道我总是可以使用 grobs 以某种方式组合它们,但如果可能的话我想使用 facet_grid
组合它们。可能类似于:
poly2 <- df
poly2$degree <- 2
poly3 <- df
poly3$degree <- 3
poly4 <- df
poly4$degree <- 4
polyn <- rbind(poly2,poly3,poly4)
ggplot(polyn,aes(x=x,y=y)) + stat_smooth(method="lm",formula=y~poly(x,degree)) +
facet_grid(degree~.)
这当然行不通,因为分面在 y~poly(x,degree)
上不起作用,因此 degree
从数据中提取。有什么办法可以做到这一点吗?
您始终可以手动预测点,然后很容易地进行分面,
## Data
set.seed(0)
df <- data.frame(x=rep(1:10,each=10),y=rnorm(100))
## Get poly fits
dat <- do.call(rbind, lapply(1:4, function(d)
data.frame(x=(x=runif(1000,0,10)),
y=predict(lm(y ~ poly(x, d), data=df), newdata=data.frame(x=x)),
degree=d)))
ggplot(dat, aes(x, y)) +
geom_point(data=df, aes(x, y), alpha=0.3) +
geom_line(color="steelblue", lwd=1.1) +
facet_grid(~ degree)
要添加置信区间,您可以将选项 interval='confidence'
与 predict
结合使用。您可能还对函数 ggplot2::fortify
感兴趣,以获得更多拟合统计数据。
dat <- do.call(rbind, lapply(1:4, function(d) {
x <- seq(0, 10, len=100)
preds <- predict(lm(y ~ poly(x, d), data=df), newdata=data.frame(x=x), interval="confidence")
data.frame(cbind(preds, x=x, degree=d))
}))
ggplot(dat, aes(x, fit)) +
geom_point(data=df, aes(x, y), alpha=0.3) +
geom_line(color="steelblue", lwd=1.1) +
geom_ribbon(aes(x=x, ymin=lwr, ymax=upr), alpha=0.3) +
facet_grid(~ degree)
我有一个非常丑陋的解决方案,其中 de plot 是多面的,并且为适当的数据子集绘制了拟合:
p1 <- ggplot(polyn,aes(x=x,y=y)) + facet_grid(.~degree)
p1 +
stat_smooth(data=polyn[polyn$degree==2,],formula=y~poly(x,2),method="lm") +
stat_smooth(data=polyn[polyn$degree==3,],formula=y~poly(x,3),method="lm") +
stat_smooth(data=polyn[polyn$degree==4,],formula=y~poly(x,4),method="lm")
产量