2016-08-12 11 views
0

私は3つの折れ線グラフのファセットプロットを作成しようとしています。私は "oirf"に対応する行を実線にしたいと思います。また、 "upperCI"と "lowerCI"に対応する両方の行は破線で表示されます。 :ggplotエラー:3つの変数の折れ線グラフを作成し、それらのファセットプロットを作成します。

hand-drawn line plots

しかし、私は解決できないエラーを取得しています。

"countrySpellId","iso","country","step","indicator","vals" 
38,"BLR","Belarus",0,"oirf",-0.19979745478058 
38,"BLR","Belarus",1,"oirf",-0.182586795026907 
38,"BLR","Belarus",2,"oirf",-0.242312010909111 
106,"GEO","Georgia",0,"oirf",-0.154580915298088 
106,"GEO","Georgia",1,"oirf",-0.0572862343086547 
106,"GEO","Georgia",2,"oirf",-0.345457860190889 
167,"KGZ","Kyrgyzstan",0,"oirf",0.960777168532119 
167,"KGZ","Kyrgyzstan",1,"oirf",0.458003383067036 
167,"KGZ","Kyrgyzstan",2,"oirf",0.190725669245905 
38,"BLR","Belarus",0,"lowerCI",-0.357909781851253 
38,"BLR","Belarus",1,"lowerCI",-0.411483619567094 
38,"BLR","Belarus",2,"lowerCI",-0.514508124910321 
106,"GEO","Georgia",0,"lowerCI",-0.323219475085121 
106,"GEO","Georgia",1,"lowerCI",-0.236286319570866 
106,"GEO","Georgia",2,"lowerCI",-0.540228716700013 
167,"KGZ","Kyrgyzstan",0,"lowerCI",0.448913075973564 
167,"KGZ","Kyrgyzstan",1,"lowerCI",0.0581860926615476 
167,"KGZ","Kyrgyzstan",2,"lowerCI",-0.235580302805703 
38,"BLR","Belarus",0,"upperCI",-0.0416851277099078 
38,"BLR","Belarus",1,"upperCI",0.0463100295132811 
38,"BLR","Belarus",2,"upperCI",0.0298841030920997 
106,"GEO","Georgia",0,"upperCI",0.0140576444889454 
106,"GEO","Georgia",1,"upperCI",0.121713850953557 
106,"GEO","Georgia",2,"upperCI",-0.150687003681766 
167,"KGZ","Kyrgyzstan",0,"upperCI",1.47264126109067 
167,"KGZ","Kyrgyzstan",1,"upperCI",0.857820673472524 
167,"KGZ","Kyrgyzstan",2,"upperCI",0.617031641297513 

は、ここで私はプロットをやろうとしている方法は次のとおりです:

ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, group = countrySpellId, 
            stat = "identity")) + 
         geom_line(aes(linetype = indicator)) + 
         xlab("Month") + ylab("Percent change") + 
         theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) + 
         scale_linetype_discrete(name ="indicator", 
         breaks=c("lowerCI", "oirf","upperCI")) + 
        facet_wrap(~ country, scales = "free_y", nrow = 3) 

をしかし、私は何とか私が思うaes(linetype = indicator)に関連して、このエラーを取得

は、ここに私のデータです。

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

私は間違っていますか?

+0

画像は、あなたがそれでいる間 –

+0

があなたの 'aes'(と' stat'から 'group'を削除するまで表示されません)。 –

答えて

1

リチャード(linetype引数は、基本的にgroupと同じことをしますが、二つの異なる変数に二回のグループを適用しようとしているためgroup=countrySpellIdは、エラーを解消します削除すると、それだけでは異なるラインを意味指摘したように、異なる線種も有する)。国の分類は後でfacet_wrapで行われます。

ggplotが自動的に選択する3種類の線種が表示されますが、それらの線がどのように表示されるのかが特に分かりますから、scale_linetype_manualを使用して、ggplotが割り当てる線種を指定できます各要因レベルあなたはscale_linetype_discreteで正しい道にいました!

悪いgroup引数を削除し、我々が持っているscale_lientype_discreteを置き換える:

ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, stat = "identity")) + 
    geom_line(aes(linetype = indicator)) + 
    xlab("Month") + ylab("Percent change") + 
    theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) + 
    scale_linetype_manual(name = "indicator", values = c(2,1,2)) + 
    facet_wrap(~ country, scales = "free_y", nrow = 3) 
+0

非常に明確で、非常に高く評価されています。私はまた、R Graphics Cookbook [リンク](https://rpubs.com/escott8908/RGC_Ch4_Line_Graphs)のレシピに従うことで、すべての集まりがなくてもこの特定のプロットを作成できることに気付きました。 – Irix3537106

関連する問題