Skip to content

Instantly share code, notes, and snippets.

@nozma
Created January 22, 2017 13:13
Show Gist options
  • Select an option

  • Save nozma/facd12297421177fc585b400ca5aa5e0 to your computer and use it in GitHub Desktop.

Select an option

Save nozma/facd12297421177fc585b400ca5aa5e0 to your computer and use it in GitHub Desktop.
ggplot2で分割したグラフ毎に異なるtableGrobを書き込む ref: http://qiita.com/nozma/items/a87bbca80b77b3abd161
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~drv) +
theme_bw() +
theme(panel.grid = element_blank()) +
geom_smooth(method = "lm")
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
))
## 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