2017-08-02 8 views
1

ファセットバープロットが必要です。合計値を示すバーは赤で、その他は青でなければなりません。
合計値はDesc1 = " - "で示されます。 私は次のコードを持っている:ファセットバープロットがカラー値を尊重していません

#df contains the following data:                           
#Year;Maingroup;Desc1 ;Desc2  ;Value 
#2017;A    ;1  ;A1    ;10 
#2017;A    ;2  ;A2    ;20 
#2017;A    ;-  ;AT    ;30 
#2017;B    ;10  ;B10   ;100 
#2017;B    ;20  ;B20   ;200 
#2017;B    ;-  ;BT    ;300                          
#2017;C    ;11  ;C100   ;53 
#2017;C    ;22  ;C200   ;54 
#2017;C    ;-  ;CT    ;107 

#add a new description                             
df <- df %>% unite (Oms,Desc1,Value,sep=' ',remove=FALSE) 
#add color indication 
df <- df %>% mutate(colv=(ifelse(Desc1=="-  ","Red","Blue"))) 
#sort into descending order 
df <- df %>% arrange(desc(Value)) 
#plot horizontal barchart 
ggplot(df, aes(x=Desc2,y=Value))+ 
geom_bar(position=position_dodge(),stat="identity",fill=df$colv)+ 
facet_grid(~Maingroup,scale="free")+ 
coord_flip() 

をしかし、Rは、正しい方法でバーを着色されていません。 Wrong bar colors 何が間違っていますか?

+1

私はあなたが '埋める=を置く必要があると思います。.. '' aes() 'コールの中で –

+0

スーパークイックレスポンスをお寄せいただきありがとうございましたが、それはうまくいかなかった – Rense

+0

次に、' dput'を使って実際のデータの最小限の例を(処理後に)投稿することをお勧めします何が悪いのか試してみる –

答えて

0

あなたは、このような色を定義するための()scale_fill_manualを使用し、その後初めで、AES()内の塗り= colvを使用し、する必要があります。

ggplot(df, aes(x=Desc2,y=Value, fill=colv))+ 
geom_bar(position=position_dodge(),stat="identity") + 
scale_fill_manual(values = c("blue", "red")) + 
facet_grid(~Maingroup,scale="free") + 
coord_flip() 
関連する問題