1
dput(x)
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), .Label = c("1/1/2012", "2/1/2012", "3/1/2012"
), class = "factor"), Server = structure(c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
    Storage = c(10000L, 20000L, 30000L, 15000L, 15000L, 25000L, 
    35000L, 15700L, 16000L, 27000L, 37000L, 16700L)), .Names = c("Date", 
"Server", "Storage"), class = "data.frame", row.names = c(NA, 
-12L))

スタック バー x=日付、y=ストレージを作成し、合計ストレージに基づいて直線を配置したいと考えています。

私はこのggplot行を思いつきました:

ggplot(x, aes(x=Date, y=Storage)) + geom_bar(aes(x=Date,y=Storage,fill=Server), stat="identity", position="stack") + geom_smooth(aes(group=1),method="lm", size=2, color="red")

それはちょっと機能しますが、直線は日付フレーム x の特定の日付の合計ストレージに基づいていません。これを行う簡単な方法はありますか?

4

1 に答える 1

1

多くの場合、最も簡単な方法は、ggplot2 の外部で値を計算することです。したがって、合計を計算します。

dd = as.data.frame(tapply(x$Storage, x$Date, sum))
dd$Date = rownames(dd)
colnames(dd)[1] = "Storage"

次に呼び出しを追加しgeom_smoothますが、データを指定します。

ggplot(x, aes(x=Date, y=Storage)) + 
    geom_bar(aes(x=Date,y=Storage, fill=Server), stat="identity", position="stack") + 
    geom_smooth(data = dd, aes(x=Date, y=Storage, group=1),method="lm")
于 2013-02-04T14:59:28.127 に答える