1

簡単な質問かもしれませんが、私にはわかりません。それは私の元の df です:

                Date.time      P    ID
    1   2013-07-03 11:15:00 1207.7  K0904
    2   2013-07-03 11:20:00 1207.7  K0904
    3   2013-07-03 11:25:00 1207.9  K0904
    4   2013-07-03 11:30:00 1208.0  K0904
    5   2013-07-03 11:35:00 1208.0  K0904
    ....
    70  2013-07-03 17:00:00 1208.6  K0955
    71  2013-07-03 17:05:00 1208.4  K0955
    72  2013-07-03 17:10:00 1208.4  K0955
    73  2013-07-03 17:15:00 1208.6  K0955
    74  2013-07-03 17:20:00 1208.8  K0955
    ...

そして、このコードで

    ggplot(df1, aes(x=Date.time, y=P, group=ID, color=ID)) + 
      geom_line() +
      facet_grid(~ID) +
      theme(legend.position="none")

私はこのプロットを作成します

ここに画像の説明を入力

単純に、最後の 3 つのプロットを最初の 3 つのプロットにオーバーレイしたい (そのため、プロットは小さくなり、読みやすくなります。df を 2 つに分割しようとしました。それぞれが 3 つの ID で作成され、それらを結合してgeom_line()、df ごとに 2 つを追加しました)新しい df を抽出するコードは次のとおりです。

df1<-subset(df, ID %in% c("K1142", "K0904", "K1136"))
df2<-subset(df, ID %in% c("K0955", "K1148", "K8651"))

そして新たな試み

ggplot(df1, aes(x=Date.time, y=P, group=ID, color=ID)) + 
  geom_line() +
  geom_line(data=df2, aes(x=Date.time, y=P, color=ID)) +
  facet_grid(~ID) +
  theme(legend.position="none")
4

1 に答える 1

5

これでうまくいきます(基本的に@MattParkerが提案したもの)

# Recreate your data (fake)
Date.time <- rep(1:100, 6)
P <- runif(600) + rep(0:5, each=100)
ID <- gl(6, 100, labels=c("K1142", "K0904", "K1136", "K0955", "K1148", "K8651"))
d <- data.frame(Date.time=Date.time, P=P, ID=ID)


#Create a new dummy variable to facet by
#    (There's a cleaner way to do this, but this way is easy to understand)
d$newID <- NA
d$newID[d$ID %in% levels(d$ID)[1:2]] <- "groupA"
d$newID[d$ID %in% levels(d$ID)[3:4]] <- "groupB"
d$newID[d$ID %in% levels(d$ID)[5:6]] <- "groupC"

# Make the plot, faceting on the dummy variable
ggplot(d, aes(x=Date.time, y=P, colour=ID)) + geom_line() +
  facet_wrap(~newID)
于 2013-08-15T20:45:19.310 に答える