2016-12-07 29 views
0

私はgeom_linesを滑らかにし、その間を埋めるようにしたいと思います。私はstat_smooth()とgeom_ribbon()とgeom_polygon()の両方を滑らかにすることを試みましたが、成功しませんでした。ggplot2:スムーズで塗りつぶし

二重バレルの質問に対する謝罪。

Smooth and fill

bell <- data.frame(
       month = c("Launch","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th"), 
       rate = c(0,.05,.12,.18,.34,.42,.57,.68,.75,.81,.83,.85,.87)) 
bell$month <- factor(bell$month, levels = rev(c("Launch","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th"))) 

ggplot() + 
     theme_minimal() + 
     coord_flip() + 
     scale_fill_manual(values=cols) + 
     geom_line(data=bell, aes(x=month, y=.5-(rate/2), group=1), color='pink', size=1) + 
     geom_line(data=bell, aes(x=month, y=.5+(rate/2), group=1), color='pink', size=1) + 
     theme(legend.position='none', axis.ticks=element_blank(), axis.text.x=element_blank(),axis.title.x=element_blank()) 

答えて

1

1つの選択肢は、ggplotの外側loess回帰のポイントを計算し、次に充填領域に対してgeom_line(行の場合)またはgeom_areaを使用してプロット(geom_areageom_ribbonあるが、としますyminはゼロに固定される)。

また、coord_flipは不要です。代わりに、xyのマッピングを切り替えてください。これは、カーブの下に塗りつぶしたい場合は、とにかく必要です。

次の例では、回帰の数値変数を作成しました。私はscale_fill_manual行をコメントアウトしました。あなたの例はcolsベクトルを提供せず、プロットコードはとにかく凡例を生成しないからです。私はまた、legend.position='none'行を余分なものとしてコメントアウトしました。

bell$month.num = 0:12 

m1 = loess(rate ~ month.num, data=bell) 
bell$loess.mod = predict(m1) 

ggplot(bell, aes(y=month, group=1)) + 
    theme_minimal() + 
    #scale_fill_manual(values=cols) + 
    geom_area(aes(x=.5-(loess.mod/2)), fill='pink', size=1) + 
    geom_area(aes(x=.5+(loess.mod/2)), fill='pink', size=1) + 
    theme(#legend.position='none', 
     axis.ticks=element_blank(), 
     axis.text.x=element_blank(), 
     axis.title.x=element_blank()) 

enter image description here

関連する問題