【问题标题】:R ggplot modifying mapping aesR ggplot 修改映射 aes
【发布时间】:2019-01-02 23:28:12
【问题描述】:

我正在尝试动态更新 ggplot 对象的映射,但不知道如何去做。

another post of stackoverflow 似乎解决了大部分问题,但我不知道如何动态命名 aes 映射...

    # start of mapping:
mapping <- aes(x = X, y = Y, col = COLOUR)

这给出了:

> mapping
Aesthetic mapping: 
* `x`      -> `X`
* `y`      -> `Y`
* `colour` -> `COLOUR`

然后我想在 aes 函数中添加更多映射。

# new things that I want to add to mapping:
new_mapping_names <- letters[1:4]

# function that gets most of the way there:
#   fun from: https://stackoverflow.com/questions/21748598/add-or-override-aes-in-the-existing-mapping-object
add_modify_aes <- function(mapping, ...) {
  ggplot2:::rename_aes(modifyList(mapping, ...))  
}

# loop to try add them in one by one:
for(new_mapping in new_mapping_names){
  # things i've tried:
  # mapping <- add_modify_aes(mapping, aes_string(!!sym(new_mapping) = paste(new_mapping)))
  # mapping <- add_modify_aes(mapping, aes_string(eval(parse(text=new_mapping)) = paste(new_mapping)))
  mapping <- add_modify_aes(mapping, aes_string(as.name(new_mapping) = new_mapping))
  # mapping[[new_mapping]] <- quo(!!new_mapping)
}
mapping

在流程结束时,我希望映射看起来像:

> mapping
Aesthetic mapping: 
* `x`      -> `X`
* `y`      -> `Y`
* `colour` -> `COLOUR`
* `a` -> `a`
* `b` -> `b`
* `c` -> `c`
* `d` -> `d`

这样做的原因是我可以将生成的 ggplot 对象 (ggplt) 传递给 ggplotly,并将映射中的任何内容用作工具提示:

df <- data.frame(X=rnorm(10),Y=rnorm(10),
                 COLOUR = sample(c('A', 'B', 'C'), 10, T),
                 a = 1:10, b=11:20, c=21:30, d=31:40)
ggplt <- ggplot(df, mapping) + geom_point()
ggplotly(ggplt, tooltip = new_mapping_names)

new_mapping_names 并不总是字母[1:4]。任何帮助将不胜感激。 干杯

【问题讨论】:

    标签: r ggplot2 aesthetics


    【解决方案1】:

    虽然我没有使用您指定的功能,但我想以下解决方案可以解决您的问题

    > df <- data.frame(X=rnorm(10),Y=rnorm(10),
    +                  COLOUR = sample(c('A', 'B', 'C'), 10, T),
    +                  a = 1:10, b=11:20, c=21:30, d=31:40)
    > mapping <- aes(x=X, y=Y, col=COLOUR)
    > df
                X           Y COLOUR  a  b  c  d
    1   0.6138723  1.61837122      B  1 11 21 31
    2   0.5259420 -0.80905208      B  2 12 22 32
    3  -0.4236438  1.41827060      C  3 13 23 33
    4   0.9877539 -0.33813806      C  4 14 24 34
    5   0.9751136  0.03876423      C  5 15 25 35
    6   0.8123134 -1.23545463      A  6 16 26 36
    7  -0.6657758  0.96099869      C  7 17 27 37
    8  -1.2342100 -1.78106632      A  8 18 28 38
    9  -0.4051921  1.22354846      A  9 19 29 39
    10  0.5225744 -0.05270590      B 10 20 30 40
    
    > mapping
    * x      -> X
    * y      -> Y
    * colour -> COLOUR
    
    > len<-length(mapping)
    > new_mapping_names <- letters[1:3]
    > new_mapping_names
    [1] "a" "b" "c"
    
    > for(i in 1:length(new_mapping_names)){
    +   mapping[i+len][[1]]<-as.name(new_mapping_names[i])#adding desired number of aesthetics and their values
    +   names(mapping)[i+len] <- new_mapping_names[i]#naming them
    + }
    
    > mapping
    * x      -> X
    * y      -> Y
    * colour -> COLOUR
    * a      -> a
    * b      -> b
    * c      -> c
    

    【讨论】:

    • 太好了。完美地完成工作。我想我需要更新一些软件包;我使用的 aes_string 版本将公式作为列表元素返回,而不是符号。欣赏!
    猜你喜欢
    • 2013-07-15
    • 2021-08-24
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多