Created
January 22, 2017 13:13
-
-
Save nozma/facd12297421177fc585b400ca5aa5e0 to your computer and use it in GitHub Desktop.
ggplot2で分割したグラフ毎に異なるtableGrobを書き込む ref: http://qiita.com/nozma/items/a87bbca80b77b3abd161
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p <- ggplot(mpg, aes(displ, hwy)) + | |
| geom_point() + | |
| facet_wrap(~drv) + | |
| theme_bw() + | |
| theme(panel.grid = element_blank()) + | |
| geom_smooth(method = "lm") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| tpar = list(bg_params = list(fill = "gray", alpha = 0.8)) # 表の外観パラメータ | |
| tg <- mpg %>% | |
| split(.$drv) %>% | |
| purrr::map(., ~ (lm(hwy ~ displ, data = .) %>% broom::tidy())[1:2]) %>% | |
| purrr::map(., ~ data.frame(coef = round(.[2], 1), | |
| row.names = c("(Intercept)", "displ"))) %>% | |
| purrr::map(., tableGrob, | |
| theme = ttheme_minimal( | |
| base_size = 10, | |
| padding = unit(c(2, 2), "mm"), | |
| core = tpar, | |
| colhead = tpar, | |
| rowhead = tpar | |
| )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## gtableの作成 | |
| g <- ggplot_gtable(ggplot_build(p)) | |
| ## facetsの取得 | |
| facets <- grep("panel", g$layout$name) | |
| ## tableGrobを書き込む | |
| g2 <- with(g$layout[facets, ], | |
| gtable_add_grob(g, tg, | |
| t=t, l=l)) | |
| plot(g2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment