追記(2015/06/28): contourとellipseの結果がうまく出てなかったので修正しました。
ggplot2で、aes()に..count..のような形の変数を指定できます。なんと呼ぶのが正しいのかよく分からずググれなくていつも困るんですが、「Generated variables」とか「Computed variables」とか言うみたいです。
https://github.com/hadley/ggplot2-book/blob/master/layers.rmd#generated-variables
使える変数名はstatによって違っていて、それぞれ使える変数は?stat_XXXXでヘルプを見ればでてきます。が、一覧でみたい、と思ってやってみました。
@koshkeさんアドバイスありがとうございました!
library(dplyr) library(magrittr) library(rlist) library(ggplot2) library(stringr) packageVersion("ggplot2") #> [1] ‘1.0.1.9000’ # 適当なデータをつっこんで、生成される変数名を返す関数 get_generated_variables <- function(stat_func){ dummy_data <- data.frame(a = 1:25, x_grid = rep(1:5, 5), y_grid = rep(1:5, each = 5), z_grid = 1:25 %% 3) stat_obj <- stat_func() required_aes <- stat_obj$stat$required_aes dummy_aes_args <- list() dummy_aes_args[required_aes] <- "a" if (stat_obj$stat$objname == "contour") { dummy_aes_args["x"] <- "x_grid" dummy_aes_args["y"] <- "y_grid" dummy_aes_args["z"] <- "z_grid" } else if (stat_obj$stat$objname == "ellipse") { dummy_aes_args["y"] <- "y_grid" } dummy_aes <- do.call(aes_string, dummy_aes_args) nms <- (ggplot(data = dummy_data, mapping = dummy_aes) + stat_obj) %>% ggplot_build %>% use_series(data) %>% extract2(1) %>% names setdiff(nms, c(required_aes, "PANEL")) } # main -------------------------------------------------------------------------------- # stat_XXXという名前の関数を取得 ### stat_function, stat_vline, stat_hlineはそれぞれデフォルトで指定しなければいけない引数があるので取り除く stat_funcs <- ls('package:ggplot2', pattern = "^stat_.+") %>% extract({is_in(., c("stat_function", "stat_hline", "stat_vline")) %>% `n'est pas`}) %>% list.map(get(., envir = asNamespace('ggplot2'))) # 必要なマッピング required_aes <- stat_funcs %>% list.map(.()$stat$required_aes) %>% list.map(inset(list(), ., "★")) # 計算される変数 generated_variables <- stat_funcs %>% list.map(get_generated_variables(.)) %>% list.map(inset(list(), ., "○")) # 結果をマージ merged <- list.maps( plyr::defaults(..1, ..2), generated_variables, required_aes) %>% list.map(as_data_frame(.)) %>% bind_rows # 出力 -------------------------------------------------------------------------------- # 見やすいようにNAには空文字を入れる merged[is.na(merged)] <- "" merged <- merged[,sort(colnames(merged))] rownames(merged) <- str_replace(names(stat_funcs), "stat_", "") knitr::kable(t(merged))
結果、ここに入らない。。「★」がrequiredな変数で、「○」が自動で生成される変数です。
| abline | bin | bin2d | bindot | binhex | boxplot | contour | density | density2d | ecdf | ellipse | identity | quantile | smooth | spoke | sum | summary | summary_hex | summary2d | unique | ydensity | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| angle | ★ | |||||||||||||||||||||
| binwidth | ○ | |||||||||||||||||||||
| count | ○ | ○ | ○ | ○ | ○ | ○ | ||||||||||||||||
| countidx | ○ | |||||||||||||||||||||
| density | ○ | ○ | ○ | ○ | ○ | |||||||||||||||||
| fill | ○ | ○ | ○ | ○ | ||||||||||||||||||
| group | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | |
| intercept | ○ | |||||||||||||||||||||
| level | ○ | ○ | ||||||||||||||||||||
| lower | ○ | |||||||||||||||||||||
| middle | ○ | |||||||||||||||||||||
| n | ○ | ○ | ○ | |||||||||||||||||||
| ncount | ○ | ○ | ||||||||||||||||||||
| ndensity | ○ | |||||||||||||||||||||
| notchlower | ○ | |||||||||||||||||||||
| notchupper | ○ | |||||||||||||||||||||
| order | ○ | |||||||||||||||||||||
| outliers | ○ | |||||||||||||||||||||
| piece | ○ | ○ | ||||||||||||||||||||
| prop | ○ | |||||||||||||||||||||
| quantile | ○ | |||||||||||||||||||||
| radius | ★ | |||||||||||||||||||||
| sample | ★ | |||||||||||||||||||||
| scaled | ○ | ○ | ||||||||||||||||||||
| se | ○ | |||||||||||||||||||||
| size | ○ | |||||||||||||||||||||
| slope | ○ | |||||||||||||||||||||
| stackpos | ○ | |||||||||||||||||||||
| theoretical | ○ | |||||||||||||||||||||
| upper | ○ | |||||||||||||||||||||
| value | ○ | ○ | ||||||||||||||||||||
| violinwidth | ○ | |||||||||||||||||||||
| weight | ○ | |||||||||||||||||||||
| width | ○ | ○ | ||||||||||||||||||||
| x | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ○ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | |||
| xbin | ○ | ○ | ||||||||||||||||||||
| xend | ○ | |||||||||||||||||||||
| xint | ○ | ○ | ||||||||||||||||||||
| xmax | ○ | ○ | ○ | ○ | ○ | ○ | ○ | |||||||||||||||
| xmin | ○ | ○ | ○ | ○ | ○ | ○ | ○ | |||||||||||||||
| y | ○ | ★ | ○ | ★ | ★ | ★ | ○ | ★ | ○ | ★ | ○ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | |||
| ybin | ○ | ○ | ||||||||||||||||||||
| yend | ○ | |||||||||||||||||||||
| yint | ○ | ○ | ||||||||||||||||||||
| ymax | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ||||||||||||||
| ymax_final | ○ | |||||||||||||||||||||
| ymin | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ||||||||||||||
| ymin_final | ○ | |||||||||||||||||||||
| z | ★ | ★ | ★ |