简体   繁体   English

如何用facet_grid在ggplot2中显示不同的度多项式拟合?

[英]How can I show different degree polynomial fits in ggplot2 with facet_grid?

I want to use facets (because I like the way they look for this) to show polynomial fits of increasing degree. 我想使用facets(因为我喜欢他们寻找的方式)来显示越来越多的多项式拟合。 It's easy enough to plot them separately as follows: 如下所示,分别绘制它们很容易:

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

I know I can always combine them in some fashion using grobs, but I would like to combine them using facet_grid if possible. 我知道我总是可以使用grobs以某种方式组合它们,但我想尽可能使用facet_grid将它们组合起来。 Maybe something similar to: 也许类似于:

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~.)

This doesn't work, of course, because the faceting does not work on y~poly(x,degree) so that degree gets pulled from the data. 当然,这不起作用,因为刻面在y~poly(x,degree)上不起作用y~poly(x,degree)因此从数据中提取degree Is there some way to make this work? 有没有办法使这项工作?

You can always predict the points manually and then facet quite easily, 您可以随时手动预测点,然后很容易地预测,

## 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)

在此输入图像描述

To add confidence bands, you can use the option interval='confidence' with predict . 要添加置信interval='confidence' ,可以使用选项interval='confidence'predict You might also be interested in the function ggplot2::fortify to get more fit statistics. 您可能还对ggplot2::fortify函数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)

在此输入图像描述

I have a very ugly solution, in which de plot is faceted and the fits are plotted for the appropriate subsets of the data: 我有一个非常丑陋的解决方案,其中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")

yields 产量 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM