Add or override aes in the existing mapping object

前端 未结 1 1306
感情败类
感情败类 2021-01-02 11:19

Here\'s the minimal case:

df <- data.frame(x=1:5, y=1, col=1:5)
mapping <- aes(x=x, y=y)
ggplot(df, mapping) + geom_point(size=10)

N

相关标签:
1条回答
  • 2021-01-02 12:05

    Based on @koshke's comment, here's a working example that does the job:

    df <- data.frame(x=1:5, y=1, new_y=5:1, col=1:5, new_col=factor(1:5))
    mapping <- aes(x=x, y=y, col=col)
    ggplot(df, mapping) + geom_point(size=10)
    
    add_modify_aes <- function(mapping, ...) {
      ggplot2:::rename_aes(modifyList(mapping, ...))  
    }
    
    ggplot(df, add_modify_aes(mapping, aes(color=new_col, y=new_y))) + geom_point(size=10)
    

    There's a slight modification that deals with aes collisions (i.e., col, color, colour).

    Initial plot:enter image description here Modified plot:enter image description here

    0 讨论(0)
提交回复
热议问题