2016-09-30 129 views
2

df由两个正常的变量时:ab,一个颜色变量:c和一组变量:d错误颜色使用facet_wrap()

df<-data.frame(c(1:5,11:15),c(1:10),c(1,1,1,1,1,2,1,2,1,2),c(rep('o',5),rep('m',5))) 
colnames(df)<-c('a','b','c','d') 

我想有om基团在一个情节,所以我做到以下几点:

qplot(a,b,data=df,geom = 'point',color=factor(df$c))+facet_wrap(~d)+scale_colour_manual(values=c("blue","orange")) 

enter image description here 积结果的颜色根据df,组m应该有蓝色和橙色点。 这是为什么发生?我怎么能做正确的情节?

此问题与Behavior ggplot2 aes() in combination with facet_grid() when passing variable with dollar sign notation to aes()类似。我的情况表明,从未使用$[不仅aes()facet_wrap()

回答

3

不要在ggplot美学使用$符号。改用原始变量名称:

qplot(a,b,data=df,geom='point',color=factor(c)) + 
    facet_wrap(~d) + 
    scale_colour_manual(values=c("blue","orange")) 
+0

这真的很有帮助,非常感谢!顺便说一下,如何将这两个组以两行排列而不是一行与上面绘制的两个colume进行比较? –

+1

这应该工作:facet_wrap(facets,** nrow = NULL,ncol = NULL **,scales =“fixed”,shrink = TRUE,labeller =“label_value”,as.table = TRUE,switch = NULL,drop = TRUE ,dir =“h”) –

+1

阅读?facet_wrap并记下nrow参数。 – Axeman