Home > database >  R ggplot2 aes arguments - How do I know what arguments are valid?
R ggplot2 aes arguments - How do I know what arguments are valid?

Time:07-11

I am learning R and I was not able to find a comprehensive list of arguments wich I can put into the aes() call. The RStudio help just states aes(x, y, ...) but that "...": How do I know what can be put there?

I found by calling ??aes I can get a good overview of what is possible (see here https://bookdown.org/yih_huynh/Guide-to-R-Book/basic-aesthetics.html ).

But I also know from a course I can use arguments like "group". Example

library(dplyr)
library(ggplot2)
library(dslabs)
data(heights)
p <- heights %>% 
  ggplot(aes(height, group = sex))  
  geom_density() 

So the question is: Is there a full list of arguments wich I can use in the aesthetic mappings?

CodePudding user response:

As said by @Limey, it depends on the geom you use. In this post (Is there a table or catalog of aesthetics for ggplot2?), @moodymudskipper gave a really nice answer to see the arguments in the aesthetics. Here a reproducible example:

library(ggplot2)
library(dplyr)
library(purrr)
env <- asNamespace("ggplot2")
all_geoms <- ls(envir = env, pattern = "^Geom. ")
all_geoms <- mget(all_geoms, env)

all_aes <- map(all_geoms, ~.$aesthetics())
all_aes
#> $GeomAbline
#> [1] "slope"     "intercept" "colour"    "size"      "linetype"  "alpha"    
#> [7] "group"    
#> 
#> $GeomAnnotationMap
#> [1] "colour"   "fill"     "size"     "linetype" "alpha"    "subgroup" "group"   
#> 
#> $GeomArea
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomBar
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomBlank
#> [1] "group"
#> 
#> $GeomBoxplot
#>  [1] "x"        "y"        "lower"    "xlower"   "upper"    "xupper"  
#>  [7] "middle"   "xmiddle"  "ymin"     "xmin"     "ymax"     "xmax"    
#> [13] "weight"   "colour"   "fill"     "size"     "alpha"    "shape"   
#> [19] "linetype" "group"   
#> 
#> $GeomCol
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomContour
#> [1] "x"        "y"        "weight"   "colour"   "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomContourFilled
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "subgroup" "group"   
#> 
#> $GeomCrossbar
#>  [1] "x"        "y"        "ymin"     "xmin"     "ymax"     "xmax"    
#>  [7] "colour"   "fill"     "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomCurve
#> [1] "x"        "y"        "xend"     "yend"     "colour"   "size"     "linetype"
#> [8] "alpha"    "group"   
#> 
#> $GeomCustomAnn
#> [1] "xmin"  "xmax"  "ymin"  "ymax"  "group"
#> 
#> $GeomDensity
#> [1] "x"        "y"        "fill"     "weight"   "colour"   "alpha"    "size"    
#> [8] "linetype" "group"   
#> 
#> $GeomDensity2d
#> [1] "x"        "y"        "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomDensity2dFilled
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "subgroup" "group"   
#> 
#> $GeomDotplot
#> [1] "x"        "y"        "colour"   "fill"     "alpha"    "stroke"   "linetype"
#> [8] "group"   
#> 
#> $GeomErrorbar
#>  [1] "x"        "y"        "ymin"     "xmin"     "ymax"     "xmax"    
#>  [7] "colour"   "size"     "linetype" "width"    "alpha"    "group"   
#> 
#> $GeomErrorbarh
#> [1] "xmin"     "xmax"     "y"        "colour"   "size"     "linetype" "height"  
#> [8] "alpha"    "group"   
#> 
#> $GeomFunction
#> [1] "x"        "y"        "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomHex
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomHline
#> [1] "yintercept" "colour"     "size"       "linetype"   "alpha"     
#> [6] "group"     
#> 
#> $GeomLabel
#>  [1] "x"          "y"          "label"      "colour"     "fill"      
#>  [6] "size"       "angle"      "hjust"      "vjust"      "alpha"     
#> [11] "family"     "fontface"   "lineheight" "group"     
#> 
#> $GeomLine
#> [1] "x"        "y"        "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomLinerange
#>  [1] "x"        "y"        "ymin"     "xmin"     "ymax"     "xmax"    
#>  [7] "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomLogticks
#> [1] "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomMap
#> [1] "map_id"   "colour"   "fill"     "size"     "linetype" "alpha"    "subgroup"
#> [8] "group"   
#> 
#> $GeomPath
#> [1] "x"        "y"        "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomPoint
#> [1] "x"      "y"      "shape"  "colour" "size"   "fill"   "alpha"  "stroke"
#> [9] "group" 
#> 
#> $GeomPointrange
#>  [1] "x"        "y"        "ymin"     "xmin"     "ymax"     "xmax"    
#>  [7] "colour"   "size"     "linetype" "shape"    "fill"     "alpha"   
#> [13] "stroke"   "group"   
#> 
#> $GeomPolygon
#> [1] "x"        "y"        "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "subgroup" "group"   
#> 
#> $GeomQuantile
#> [1] "x"        "y"        "weight"   "colour"   "size"     "linetype" "alpha"   
#> [8] "group"   
#> 
#> $GeomRaster
#> [1] "x"     "y"     "fill"  "alpha" "group"
#> 
#> $GeomRasterAnn
#> [1] "group"
#> 
#> $GeomRect
#>  [1] "xmin"     "xmax"     "ymin"     "ymax"     "colour"   "fill"    
#>  [7] "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomRibbon
#>  [1] "x"        "y"        "ymin"     "xmin"     "ymax"     "xmax"    
#>  [7] "colour"   "fill"     "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomRug
#> [1] "colour"   "size"     "linetype" "alpha"    "x"        "y"        "group"   
#> 
#> $GeomSegment
#> [1] "x"        "y"        "xend"     "yend"     "colour"   "size"     "linetype"
#> [8] "alpha"    "group"   
#> 
#> $GeomSf
#> [1] "geometry" "shape"    "colour"   "fill"     "size"     "linetype" "alpha"   
#> [8] "stroke"   "group"   
#> 
#> $GeomSmooth
#>  [1] "x"        "y"        "colour"   "fill"     "size"     "linetype"
#>  [7] "weight"   "alpha"    "ymin"     "ymax"     "group"   
#> 
#> $GeomSpoke
#> [1] "x"        "y"        "angle"    "radius"   "colour"   "size"     "linetype"
#> [8] "alpha"    "group"   
#> 
#> $GeomStep
#> [1] "x"        "y"        "colour"   "size"     "linetype" "alpha"    "group"   
#> 
#> $GeomText
#>  [1] "x"          "y"          "label"      "colour"     "size"      
#>  [6] "angle"      "hjust"      "vjust"      "alpha"      "family"    
#> [11] "fontface"   "lineheight" "group"     
#> 
#> $GeomTile
#>  [1] "x"        "y"        "fill"     "colour"   "size"     "linetype"
#>  [7] "alpha"    "width"    "height"   "group"   
#> 
#> $GeomViolin
#> [1] "x"        "y"        "weight"   "colour"   "fill"     "size"     "alpha"   
#> [8] "linetype" "group"   
#> 
#> $GeomVline
#> [1] "xintercept" "colour"     "size"       "linetype"   "alpha"     
#> [6] "group"

Created on 2022-07-11 by the reprex package (v2.0.1)

CodePudding user response:

As said by @Limey in the comments, there's no comprehensive list because the possible arguments in aes() depend on which geom_ you call. For example, ?geom_line / ?geom_path give:

geom_path() understands the following aesthetics (required aesthetics are in bold):

  • x
  • y
  • alpha
  • colour
  • group
  • linetype
  • size

These functions have a linetype argument in aes() because they draw lines, but it wouldn't make sense to have a linetype arg for geom_bar for instance.

  • Related