2017-04-02 21 views
0

如何傳遞ggplot中的數據框的列名facet_wrap或函數中的fill/color?我擡頭看着lazyeval,但沒有找到辦法。如何在函數中傳遞ggplot中的列名facet_wrap

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

在這個例子中,爲什麼點不被x着色?

+2

可能的重複http://stackoverflow.com/questions/21588096/pass-string-to-facet-grid-ggplot2或http://stackoverflow.com/questions/11028353/passing-string-variable-facet-實際上使用了ggplot-using-r – akrun

回答

1

我用了ggplot2::facet_wrap的一個例子。所以,你可以很容易地用x代替~class包含一個字符或公式。例如:

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

。但是每個方面的顏色怎麼樣?這似乎並不奏效。 ggplot(mpg,aes(displ,hwy,col = x))+ geom_point()+ facet_wrap(x) 謝謝 – santoku

+0

對於每個方面的着色是什麼意思?點或背景? –

+0

要點,如片段ggplot(mpg,aes(displ,hwy,col = x))+ geom_point()+ facet_wrap(x) – santoku

0

您試過ggplot(mpg, aes(displ, hwy, col=as.factor(x))) + geom_point() + facet_wrap(~x)

相關問題