从分面绘图输出中保存单个绘图

时间:2016-03-29 21:27:32

标签: r ggplot2

我使用以下代码

生成了8个年度数据图
pl <- dlply(BC_yr, .(STATION_NAME), function(dat) {
  ggplot(data = dat, aes(x = year, y = CLDD_yr, group = STATION_NAME)) + geom_line() + 
    geom_point() + xlab("Year") + ylab("Yearly CLDD") +
    ggtitle(dat$STATION_NAME[1]) +
    geom_smooth(method = "lm")
})

pl

我现在有兴趣用正确的标题保存个别情节。

我正在使用像这样的for循环来接近这个

for(p in pl){
  ggsave(dat$STATION_NAME[1]+"plot.pdf" height = 4, width = 11, units = "in")
}

我知道我需要将输出文件格式化为str(p.STATION_NAME)+"plot.pdf"

但我无法将相应的格式传递给函数,以使输出成功。

如何在r中做到这一点?

1 个答案:

答案 0 :(得分:0)

假设您正在迭代pl的所有值,您可以试试这个:

for(p in pl){
  ggsave(paste(dat$STATION_NAME[p],"plot.pdf",sep="-") height = 4, width = 11, units = "in")
}