What is the difference between aes and aes_string (ggplot2) in R

前端 未结 1 1644
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 21:00

With missing background in informatics I have difficulties to understand the differences between aes and aes_string in ggplot2 and its implications

相关标签:
1条回答
  • 2020-12-18 21:12

    aes saves you some typing as you don't need the quotes. That is all. You are of course free to always use aes_string. You should use aes_string if you want to pass the variable names programmatically.

    Internally aes uses match.call for the non-standard evaluation. Here is a simple example for illustration:

    fun <- function(x, y) as.list(match.call())
    str(fun(a, b))
    #List of 3
    # $  : symbol fun
    # $ x: symbol a
    # $ y: symbol b
    

    For comparison:

    library(ggplot2)
    str(aes(x = a, y = b))
    #List of 2
    # $ x: symbol a
    # $ y: symbol b
    

    The symbols are evaluated at a later stage.

    aes_string uses parse to achieve the same:

    str(aes_string(x = "a", y = "b"))
    #List of 2
    # $ x: symbol a
    # $ y: symbol b
    
    0 讨论(0)
提交回复
热议问题