facet ggplot by span参数of loess smoother

时间:2015-01-02 07:31:37

标签: r ggplot2 smooth loess

我想通过将span参数改为黄土平滑器来制作单个刻面图(格子样式)。我尝试使用如下的for循环,但没有生成情节。如果我使用ggsave函数,则图表将单独保存。

另外,我想知道是否有更简约的方式来完成这样的任务?

x <- rep(1:10,4)
y <- 1.2*x + rnorm(40,0,3)
s <- seq(0.2,0.8,0.1)

# plot the series of plots by varying the span parameter
for (s_i in s) {
    qplot(x, y, geom = 'c('point','smooth'), span = s_i)
}

1 个答案:

答案 0 :(得分:3)

如果您明确print情节(并删除额外的'标记),您的方法将会正常运作:

for (s_i in s) {
    print(qplot(x, y, geom = c('point','smooth'), span = s_i))
}

至于其他方法,我建议将所有情节放在一个列表中

changing_span = list()
for (i in seq_along(s)) {
    changing_span[[i]] <- qplot(x, y, geom = c('point','smooth'), span = s[i]) +
        labs(title = paste("span:", s[i]))
}

然后你可以将所有这些一起绘制,例如,

library(gridExtra)
do.call(grid.arrange, changing_span)