12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ggplot2で、複数の時系列を一枚にプロット

Last updated at Posted at 2013-11-08
> head(dat)
           X1       X2          X3 t
1 -0.50643974 1.047231  0.27683366 1
2 -0.09770514 3.045507 -0.35219898 2
3 -1.34254553 3.454165  0.02047701 3
4 -2.80358361 3.052366 -0.35403137 4
5 -3.84976651 2.441114  0.68818969 5
6 -4.64718281 1.449005  1.68518642 6

こんな感じに、データフレームに多次元の時系列が入っていて、それらを一枚のグラフに重ねてプロットする。

melt.r
temp <- melt(dat,
             id="t",
             measure=c(
                 "X1",
                 "X2",
                 "X3"))
ggplot(temp,
       aes(x=t,
           y=value,
           colour=variable,
           group=variable)) + geom_line()

こんな時、データフレームを良い感じ(ggplot2で扱い易いよう)にしてくれる便利関数がmelt。

> head(temp)
  t variable       value
1 1       X1 -0.50643974
2 2       X1 -0.09770514
3 3       X1 -1.34254553
4 4       X1 -2.80358361
5 5       X1 -3.84976651
6 6       X1 -4.64718281

melt.jpeg

参考ページ

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?