2013-02-21 12 views
8

2つの異なるプロットをオーバーレイしようとしています。 1つはgeom_boxplot、もう1つはgeom_jitterです。私はそれぞれに独自のカラースケールを持たせたいと思います。しかし、2番目のカラースケールを追加すると、エラーが発生しますggplot2 - オーバーレイされたプロットに2つの異なるカラースケールを使用する

"Scale for 'fill' is already present. Adding another scale for 'fill', 
    which will replace the existing scale." 

私は何か間違っていると仮定しています。何かアドバイスは、これは私の作業コードのラフな一例である

をお願いすることになります。

P <- ggplot(dat) + 
      geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) + 
      scale_fill_manual(values=cpalette1) + 
      geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") + 
      theme(legend.position="none") 

P + geom_jitter(dat2, aes(x=ve, y=metValue, fill=atd), 
       size=2, shape=4, alpha = 0.4, 
       position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
       scale_fill_manual(values=cpalette2) 

datdat2は同じスキーマが、異なる値を持っています。

グラフのオーバーレイに対処する例がいくつか見つかりましたが、この特定の問題に対処するようなものはありませんでした。

答えて

11

まず、例と同じ名前の2つのサンプルデータフレームを作成しました。

dat<-data.frame(ve=rep(c("FF","GG"),times=50), 
       metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25), 
       atd=rep(c("HH","GG"),times=50)) 
dat2<-data.frame(ve=rep(c("FF","GG"),times=50), 
       metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25), 
       atd=rep(c("HH","GG"),times=50)) 

私はあなたがshape=4の色がcolour=引数で設定することもできますので、geom_jitter()に引数fill=を使用する必要がないことを前提としています。次に、scale_colour_manual()を使用して値を設定できます。 cpalleteの代わりに色の名前を使用しました。

P <- ggplot(dat) + 
    geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) + 
    geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") + 
    scale_fill_manual(values=c("red","blue","green","yellow"))+ 
    theme(legend.position="none") 

P + geom_jitter(data=dat2, aes(x=ve, y=metValue, colour=atd), 
       size=2, shape=4, alpha = 0.4, 
       position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
       scale_colour_manual(values=c("red","blue")) 

enter image description here