How to pass column name in ggplot facet_wrap in a function

两盒软妹~` 提交于 2019-12-02 17:16:34

问题


How to pass column name of a data frame in ggplot facet_wrap or fill/colour in a function? I looked up lazyeval but didn't figure out a way.

x="class"
ggplot(mpg, aes(displ, hwy, col=x)) + geom_point() + facet_wrap(x) 

In this example, why didn't the points get coloured by x?


回答1:


I used an example from ggplot2::facet_wrap. So, you can easily replace ~class with x containing a character or a formulae. Example :

library(ggplot2)
x="class"#or ~class
ggplot(mpg, aes(displ, hwy)) +
        geom_point() +
        facet_wrap(x)



回答2:


Have you tried ggplot(mpg, aes(displ, hwy, col=as.factor(x))) + geom_point() + facet_wrap(~x) ?



来源:https://stackoverflow.com/questions/43168859/how-to-pass-column-name-in-ggplot-facet-wrap-in-a-function

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