2013-04-09 61 views
0

我想比较变量的一个等级与所有其他变量的综合影响。我想用一个方面的情节做到这一点。ggplot2:你如何选择因子水平的一个子集来分组到单个方面

例如:

ggplot(diamonds, aes(price, colour = cut)) + geom_density() + facet_grid(~clarity) 

这提供了清晰的所有因子水平的方位图。但是,我想要的是第一个面的I1密度图和第二个面的〜(I1)密度图。

所以我想以使用GGPLOT2的小功能,下面的比较:

ggplot(subset(diamonds, (clarity == "I1")) , aes(price, colour = cut)) + geom_density() 

ggplot(subset(diamonds, !(clarity == "I1")) , aes(price, colour = cut)) + geom_density() 

我可以看到我怎么能在数据帧定义新的列并用其作为在facet_grid因素,但我怀疑有更好的方法来做到这一点。

+0

我认为它会更容易做出新列facet_grid使用() – 2013-04-09 12:14:26

回答

1

您可以创建一个新列(更好的解决方案),或使用gridExtra包:

library(gridExtra) 
p1 <- ggplot(subset(diamonds, (clarity == "I1")) , aes(price, colour = cut)) + geom_density() 
p2 <- ggplot(subset(diamonds, !(clarity == "I1")) , aes(price, colour = cut)) + geom_density() 
grid.arrange(p1,p2) 
相关问题