Add or override aes in the existing mapping object

南笙酒味 提交于 2019-11-30 20:07:41

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:

Modified plot:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!