是否有ggplot2的美学表或目录?

M T*_*M T 61 r ggplot2

我是ggplot2的新手,并一直试图找到一个全面的美学列表.我想我理解他们的目的,但很难知道哪些可以用于各种情况(主要是geoms?).Hadley的网站偶尔会在个别地理位置的页面上列出可用的美学,而R文档偶尔(尽管很少)会做同样的事情.我甚至发现了两个不太匹配的geom.

我在这里搜索了一些答案,甚至买了这本书!唉,没有帮助.

我认为拥有一个列在一个维度中的所有美学和所有geom(和其他对象?)在另一个维度中列出的表格会很棒.

有谁知道这样的事情?

R中是否有一个简单的方法(命令)列出可以应用于对象的所有美学?

以下是表格的开始方式:

List           x       y       fill      size    colour   linetype . . .
geom_point    Yes     Yes      Yes       Yes      Yes        No
geom_abline   Yes     Yes      No        Yes      Yes       Yes
.
.
.
Run Code Online (Sandbox Code Playgroud)

美学定义/参数目录也是非常有用的参考.

bap*_*ste 115

下面是default_aes每个geom,

            colour size linetype alpha   fill weight shape width height angle hjust vjust family fontface lineheight
abline       black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
area           yes  0.5        1   yes grey20     --    --    --     --    --    --    --     --       --         --
bar            yes  0.5        1   yes grey20      1    --    --     --    --    --    --     --       --         --
bin2d          yes  0.5        1   yes grey60      1    --    --     --    --    --    --     --       --         --
boxplot     grey20  0.5    solid   yes  white      1    16    --     --    --    --    --     --       --         --
contour    #3366FF  0.5        1   yes     --      1    --    --     --    --    --    --     --       --         --
crossbar     black  0.5        1   yes    yes     --    --    --     --    --    --    --     --       --         --
density      black  0.5        1   yes    yes      1    --    --     --    --    --    --     --       --         --
density2d  #3366FF  0.5        1   yes     --      1    --    --     --    --    --    --     --       --         --
errorbar     black  0.5        1   yes     --     --    --   0.5     --    --    --    --     --       --         --
errorbarh    black  0.5        1   yes     --     --    --    --    0.5    --    --    --     --       --         --
freqpoly     black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
hex            yes  0.5       --   yes grey50     --    --    --     --    --    --    --     --       --         --
hline        black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
linerange    black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
path         black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
point        black    2       --   yes    yes     --    16    --     --    --    --    --     --       --         --
pointrange   black  0.5        1   yes    yes     --    16    --     --    --    --    --     --       --         --
polygon         NA  0.5        1   yes grey20     --    --    --     --    --    --    --     --       --         --
quantile   #3366FF  0.5        1   yes     --      1    --    --     --    --    --    --     --       --         --
raster          --   --       --   yes grey20     --    --    --     --    --    --    --     --       --         --
rect           yes  0.5        1   yes grey20     --    --    --     --    --    --    --     --       --         --
ribbon         yes  0.5        1   yes grey20     --    --    --     --    --    --    --     --       --         --
rug          black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
segment      black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
smooth     #3366FF  0.5        1   0.4 grey60      1    --    --     --    --    --    --     --       --         --
step         black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
text         black    5       --   yes     --     --    --    --     --     0   0.5   0.5               1        1.2
tile           yes  0.1        1   yes grey20     --    --    --     --    --    --    --     --       --         --
violin      grey20  0.5    solid   yes  white      1    --    --     --    --    --    --     --       --         --
vline        black  0.5        1   yes     --     --    --    --     --    --    --    --     --       --         --
Run Code Online (Sandbox Code Playgroud)

和我曾经破解的丑陋代码,

find_aes <- function(geom="point"){

  tryCatch({
  Geom <- getFromNamespace(paste("Geom", ggplot2:::firstUpper(geom), sep=""),
                           "ggplot2")

  tmp <- unclass(Geom$default_aes)
  tmp[is.na(tmp)] <- "yes"
  data.frame(tmp, stringsAsFactors=FALSE)
  }, error = function(e) {})
}

funs <- grep("^geom_", ls("package:ggplot2"),val=T)

geoms <- gsub("^geom_", "", funs)

all <- lapply(geoms, find_aes)
names(all) <- geoms
relevant <- sapply(all, function(x) !is.null(x) && nrow(x) > 0)
library(plyr)
results = do.call("rbind.fill",all)
rownames(results) <- names(relevant[relevant])
results[is.na(results)] <- "--"

options(width=9999)
capture.output(print(results), file="aes.txt")
Run Code Online (Sandbox Code Playgroud)

  • +1!,此外,如果一个geom的默认值高于它支持那个aes,那将是真的吗?我认同... (2认同)

cho*_*tom 9

看看美学规范的小说,由Hadley Wickham写道:

此小插图总结了网格绘制功能所采用的各种格式.大部分信息可以在R文档中分散使用.本附录将所有内容集中在一个地方.

  • 死链接@ cho7tom.有工作吗?听起来不错. (3认同)

Moo*_*per 7

接受的答案仅详细说明默认美学,以下是获取所有这些的方法:

获取所有 Geom* 函数


library(tidyverse)
env <- asNamespace("ggplot2")
all_Geoms <- ls(envir = env, pattern = "^Geom.+")
all_Geoms <- mget(all_Geoms, env)
Run Code Online (Sandbox Code Playgroud)

获得所有美学

all_aes <- map(all_Geoms, ~.$aesthetics())

# change Geom* to geom_*
names(all_aes) <- 
  names(all_aes) %>%
  substr(5,nchar(.)) %>% 
  tolower() %>% 
  paste0("geom_",.)

# remove if geom_* doesn't exist
all_aes[!names(all_aes) %in% ls(envir = env)] <- NULL
head(all_aes, 3)
#> $geom_abline
#> [1] "slope"     "intercept" "colour"    "size"      "linetype"  "alpha"    
#> [7] "group"    
#> 
#> $geom_area
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype"
#> [7] "alpha"    "group"   
#> 
#> $geom_bar
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype"
#> [7] "alpha"    "group"   
Run Code Online (Sandbox Code Playgroud)

摆在一张长桌上

all_aes_long <- all_aes %>%
  enframe("fun","aes") %>%
  unchop(aes)

all_aes_long
#> # A tibble: 325 x 2
#>    fun         aes      
#>    <chr>       <chr>    
#>  1 geom_abline slope    
#>  2 geom_abline intercept
#>  3 geom_abline colour   
#>  4 geom_abline size     
#>  5 geom_abline linetype 
#>  6 geom_abline alpha    
#>  7 geom_abline group    
#>  8 geom_area   x        
#>  9 geom_area   y        
#> 10 geom_area   colour   
#> # ... with 315 more rows
Run Code Online (Sandbox Code Playgroud)

摆在一张宽大的桌子上

我建议使用View()来形象化这个。

all_aes_wide <-
  all_aes_long%>%
  mutate(val = 1) %>%
  spread(aes,val,fill = 0)

all_aes_wide
#> # A tibble: 38 x 38
#>    fun   alpha angle colour family  fill fontface geometry group height
#>    <chr> <dbl> <dbl>  <dbl>  <dbl> <dbl>    <dbl>    <dbl> <dbl>  <dbl>
#>  1 geom~     1     0      1      0     0        0        0     1      0
#>  2 geom~     1     0      1      0     1        0        0     1      0
#>  3 geom~     1     0      1      0     1        0        0     1      0
#>  4 geom~     0     0      0      0     0        0        0     1      0
#>  5 geom~     1     0      1      0     1        0        0     1      0
#>  6 geom~     1     0      1      0     1        0        0     1      0
#>  7 geom~     1     0      1      0     0        0        0     1      0
#>  8 geom~     1     0      1      0     1        0        0     1      0
#>  9 geom~     1     0      1      0     0        0        0     1      0
#> 10 geom~     1     0      1      0     1        0        0     1      0
#> # ... with 28 more rows, and 28 more variables: hjust <dbl>,
#> #   intercept <dbl>, label <dbl>, lineheight <dbl>, linetype <dbl>,
#> #   lower <dbl>, map_id <dbl>, middle <dbl>, radius <dbl>, shape <dbl>,
#> #   size <dbl>, slope <dbl>, stroke <dbl>, subgroup <dbl>, upper <dbl>,
#> #   vjust <dbl>, weight <dbl>, width <dbl>, x <dbl>, xend <dbl>,
#> #   xintercept <dbl>, xmax <dbl>, xmin <dbl>, y <dbl>, yend <dbl>,
#> #   yintercept <dbl>, ymax <dbl>, ymin <dbl>
Run Code Online (Sandbox Code Playgroud)

奖励 : aes 的频率

table(all_aes_long$aes)
#> 
#>      alpha      angle     colour     family       fill   fontface 
#>         37          3         36          2         20          2 
#>   geometry      group     height      hjust  intercept      label 
#>          1         38          2          2          1          2 
#> lineheight   linetype      lower     map_id     middle     radius 
#>          2         33          1          1          1          1 
#>      shape       size      slope     stroke   subgroup      upper 
#>          4         35          1          4          2          1 
#>      vjust     weight      width          x       xend xintercept 
#>          2          6          2         30          2          1 
#>       xmax       xmin          y       yend yintercept       ymax 
#>          2          2         27          2          1          8 
#>       ymin 
#>          8
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。顺便问一下,为什么没有 geom_histogram 呢? (2认同)