2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ggplotのグラフを一度に保存するメモ

Posted at

さいしょに

ggplotをまとめてグラフを保存しようとして四苦八苦しました。
下のページを参考にさせてもらいましたが、コードが上手く動かなかったのと、@flatsilverさんの最終投稿が2020年だったので、新しく記事を上げさせてもらいます。

違う点

ggplotオブジェクトに対してggsaveがプラスでつながっていたのですが、これでは保存できません。(正確には1枚目は保存出来て、2枚目以降が保存できません。)

これを、iwalk()ggsave()を動かして、plot =の引数のところでggplot()でggplotオブジェクトを作る、という形をとっています。

mtcars %>%
  split(.$cyl) %>%
  iwalk(~ ggsave(
    file = paste0("cyl-", .y, ".png"),
    plot =  ggplot(.x, aes(disp, mpg)) +
            geom_point() +
            ggtitle(paste0("cyl-", .y))
  ))

.xは各要素のdata.frameを、.yはそのインデックスを指定できるようです。慣れてくるとすごく扱いやすそうですね。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?