3

全て。

stackoverflow で以前のメッセージをいくつか読み、zoo と ggplot2 のドキュメントを調べましたが、適切な答えが見つかりませんでした。

「データ」と呼ばれる動物園オブジェクトがあるとします。フラット ファイルの元のデータは次のとおりです。

Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04

ここで、この 5 つの金融商品の時系列を 1 つの折れ線グラフにプロットして、それらの進化を比較したいと思います。

ggplot2を使用したい。

誰かヒントをくれませんか?

4

3 に答える 3

9

dataが動物園のオブジェクトである場合は、これを試してください (詳細については、を参照?autoplot.zooしてください)。

p <- autoplot(data, facet = NULL)
p

linetypeまたは、おそらくこれは、同じパネル内のこの多くのシリーズでは、の自動変更がうまく見えないと思うためです。

p + aes(linetype = NULL)

ここに画像の説明を入力

于 2012-12-12T22:21:29.327 に答える
4

これを行う1つの方法は次のとおりです。

df <- read.csv(text = "Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04", na.string = "#N/A")

df$Date <- strptime(df$Date, format = "%d/%m/%Y")

zooオブジェクトを作成します。

library(zoo)
dat <- zoo(df[-1], df$Date)

オブジェクトを のデータ フレームに変換しますggplot2

df_new <- data.frame(value = as.vector(dat),
                     time = time(dat),
                     quote = rep(names(dat), each = nrow(dat)))

プロット:

library(ggplot2)
ggplot(df_new, aes(y = value, x = time, colour = quote)) + geom_line()

ここに画像の説明を入力

于 2012-12-12T21:01:53.797 に答える
1

これは、reshape からの溶解を使用した、少し異なる別の方法です。

# Read your data and format date (as proposed by Sven)

df <- read.csv(text = "Date,Quote1,Quote2,Quote3,Quote4,Quote5
18/07/2008,42.36,44.53,28.4302,44.3,42
21/07/2008,43.14,44.87,28.6186,44.83,43.27
22/07/2008,43.26,44.85,28.6056,44.86,42.84
23/07/2008,44.74,45.61,29.7558,45.69,#N/A
24/07/2008,43.99,45.14,29.2944,45.19,#N/A
25/07/2008,43.18,45.33,29.4569,45.46,43.65
28/07/2008,43.45,44.72,28.5016,44.89,43.31
29/07/2008,43.49,44.8,28.1247,44.88,42.85
30/07/2008,44.55,45.54,28.0727,45.58,43.67
31/07/2008,43.36,45.5,27.9818,45.63,43.91
01/08/2008,43.34,44.75,28.0792,44.69,43.04", na.string = "#N/A")

df$Date <- strptime(df$Date, format = "%d/%m/%Y")

library(reshape)
# reshape your data with melt
melted <- melt(df[-1])
# add dates
melted2 <- cbind(df$Date,melted)
# plot with ggplot
ggplot(melted2,aes(y = value, x = melted2[,1], color = variable)) + geom_line()

プロット

于 2012-12-12T21:45:58.663 に答える