Dash in column name yields “object not found” Error

前提是你 提交于 2019-12-02 04:12:57

Normally R tries very hard to make sure you have column names in your data.frame that can be valid variable names. Using non-standard column names (those that are not valid variable names) will lead to problems when using functions that use non-standard evaluation type syntax. When focused to use such variable names you often have to wrap them in back ticks. In the normal case

ggplot(df, aes(x, y)) + 
  geom_point(aes(col = H2-Aa)) +
  scale_color_gradient()
# Error in FUN(X[[i]], ...) : object 'H2' not found

would return an error but

ggplot(df, aes(x, y)) + 
  geom_point(aes(col = `H2-Aa`)) +
  scale_color_gradient()

would work.

You can paste in backticks if you really want

geom_point(aes_string(col = paste0("`", gene, "`")))

or you could treat it as a symbol from the get-go and use aes_q instread

geom_point(aes_q(col = as.name(gene)))

The latest release of ggplot support escaping via !! rather than using aes_string or aes_q so you could do

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