読者です 読者をやめる 読者になる 読者になる

ggplot2の各geomのstatとpositionのデフォルト

R ggplot2

探せばどっかにある気もするけど。purrrの練習も兼ねてメモ。

library(dplyr)
library(purrr)
library(ggplot2)
library(stringr)

packageVersion("ggplot2")
#> [1] ‘1.0.1.9000’

e      <- asNamespace('ggplot2')
Geoms  <- grep("^geom_.+", ls(e), value = TRUE)

# geom_XXXという名前の関数のリスト
geoms  <- 
  Geoms %>%
  map(~ get(., envir = e))

geoms_list <-
  geoms %>%
  # 引数のリスト
  map(~ formals(.)) %>%
  # NULLのやつは取り除く。あと「...」とか「map」というのもsymbolで、うまくdata.frame化できないので取り除く
  map(~ keep(., ~ !(class(.) %in% c("NULL", "name"))))

# 要素を取り除いた結果、要素数がゼロになってしまうlistがあるのでそれを取り除く
idx <-
  geoms_list %>%
  map(~ length(.) > 0) %>%
  flatten

# data.frame化する
geoms_df <-
  geoms_list[idx] %>%
  map(~ as_data_frame(.)) %>%
  bind_rows

# geom名を入れる
geoms_df$geom = str_replace(Geoms[idx], "^geom_", "")

# 出力
knitr::kable(geoms_df[c("geom", "stat", "position")])
geom stat position
abline abline identity
area identity stack
bar bin stack
bin2d bin2d identity
blank identity identity
boxplot boxplot dodge
contour contour identity
count sum identity
crossbar identity identity
curve identity identity
density density identity
density2d density2d identity
dotplot bindot identity
errorbar identity identity
errorbarh identity identity
freqpoly bin identity
hex binhex identity
histogram bin stack
hline hline identity
jitter identity jitter
line identity identity
linerange identity identity
map identity NA
path identity identity
point identity identity
pointrange identity identity
polygon identity identity
quantile quantile identity
raster identity identity
rect identity identity
ribbon identity identity
rug identity identity
segment identity identity
smooth smooth identity
step identity identity
text identity identity
tile identity identity
violin ydensity dodge
vline vline identity

追記:「そこでlambdaRですよ!」という親分のアドバイスをもらったのでr-wakalangに貼ってもらったカンペを片手に書きなおしました。

library(dplyr)
library(lambdaR)
library(ggplot2)
library(stringr)

packageVersion("ggplot2")
#> [1] ‘1.0.1.9000’

e      <- asNamespace('ggplot2')
Geoms  <- grep("^geom_.+", ls(e), value = TRUE)

geoms  <- 
  Geoms %>%
  Map_(get(._, envir = e))

geoms_list <-
  geoms %>%
  Map_(formals(._)) %>%
  Map_(x: Filter_(x, !(class(._) %in% c("NULL", "name"))))

idx <-
  geoms_list %>%
  Map_(length(._) > 0) %>%
  unlist

geoms_df <-
  geoms_list[idx] %>%
  Map_(as_data_frame(._)) %>%
  bind_rows

geoms_df$geom = str_replace(Geoms[idx], "^geom_", "")

knitr::kable(geoms_df[c("geom", "stat", "position")])