2017-01-21 9 views
0

ggplot2初心者ここ私はggplot2を用いて以下に与えられたサンプルデータの時系列を生成しようとしているペアリング時系列Rのggplot2 facet_grid

を使用します。次の短いコードは、私が欲しいものを私に与えません。

ggplot(dat,aes(x=year,y=data,fill=period,group=interaction(period, season))) + 
    geom_line()+facet_grid(season ~ ., scales="free") 

あなたは、線が不自然に見えることがわかります。シーズンごとにcufuを一緒にプロットするにはどうすればよいですか? cuの場合はredfuの場合は青を使用してください。

dat=structure(list(period = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("cu", "fu"), class = "factor"), 
     season = structure(c(2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 
     1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 
     1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L), .Label = c("DJF", "JJA", 
     "MAM", "SON"), class = "factor"), month = structure(c(7L, 
     6L, 2L, 7L, 12L, 11L, 10L, 12L, 3L, 5L, 4L, 3L, 8L, 1L, 9L, 
     8L, 7L, 6L, 2L, 7L, 12L, 11L, 10L, 12L, 3L, 5L, 4L, 3L, 8L, 
     1L, 9L, 8L), .Label = c("april", "august", "dec", "feb", 
     "jan", "july", "june", "march", "may", "nov", "oct", "sep" 
     ), class = "factor"), year = c(2001L, 2001L, 2001L, 2002L, 
     2001L, 2001L, 2001L, 2002L, 2001L, 2001L, 2001L, 2002L, 2001L, 
     2001L, 2001L, 2002L, 2001L, 2001L, 2001L, 2002L, 2001L, 2001L, 
     2001L, 2002L, 2001L, 2001L, 2001L, 2002L, 2001L, 2001L, 2001L, 
     2002L), data = c(84.08969137, 76.4948428, 18.35492802, 101.8821712, 
     24.21773903, 16.44881361, 19.57283027, 48.27623315, 8.572824549, 
     12.97601394, 11.50496081, 15.14899058, 13.96396375, 27.21030149, 
     36.1606234, 23.35430348, 95.77643784, 94.84972642, 47.26900009, 
     2.385978093, 21.48062239, 24.67779645, 20.07044416, 43.09234771, 
     13.28295078, 19.27189857, 15.24661793, 21.75991334, 19.38239851, 
     39.93109491, 38.54500325, 33.77559647)), .Names = c("period", 
    "season", "month", "year", "data"), class = "data.frame", row.names = c(NA, 
    -32L)) 

ありがとうございます。

library(ggplot2) 
ggplot(dat,aes(x= 
       as.Date(sprintf("%s-%s-01",year,month), 
         "%Y-%b-%d"), 

         y=data,group=period,color=period)) + 
    geom_line()+facet_grid(season ~ ., scales="free") + 
    xlab("time") 

実際には、私は定期的な日付を作成していますし、期間によってだけでグループ化:

+1

なぜあなたは 'むしろcolour''よりfill'を使用していますか? 'geom_line'は' fill'を無視します –

答えて

0

私のような何かをするだろう。

enter image description here

+0

素晴らしい!まさに私が探していたもの。 – code123

関連する問題