How to pass column name in ggplot facet_wrap in a

2019-03-04 15:06发布

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?

2条回答
相关推荐>>
2楼-- · 2019-03-04 15:15

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

查看更多
放荡不羁爱自由
3楼-- · 2019-03-04 15:25

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)
查看更多
登录 后发表回答