5

次のデータセットの場合、

Genre   Amount
Comedy  10
Drama   30
Comedy  20
Action  20
Comedy  20
Drama   20

Genrex軸がすべての金額の合計であり、y軸がすべての金額の合計であるggplot2折れ線グラフを作成したいと思います(条件付きGenre)。

私は次のことを試しました:

p = ggplot(test, aes(factor(Genre), Gross)) + geom_point()
p = ggplot(test, aes(factor(Genre), Gross)) + geom_line()
p = ggplot(test, aes(factor(Genre), sum(Gross))) + geom_line()

しかし、役に立たない。

4

2 に答える 2

8

プロットする前に新しいデータ フレームを計算したくない場合は、ggplot2 で使用できますstat_summary。たとえば、データ セットが次のようになっているとします。

R> df <- data.frame(Genre=c("Comedy","Drama","Action","Comedy","Drama"),
R+                  Amount=c(10,30,40,10,20))
R> df
   Genre Amount
1 Comedy     10
2  Drama     30
3 Action     40
4 Comedy     10
5  Drama     20

qplot次のいずれかをstat="summary"引数とともに使用できます。

R> qplot(Genre, Amount, data=df, stat="summary", fun.y="sum")

stat_summaryまたは、ベースggplotグラフィックに を追加します。

R> ggplot(df, aes(x=Genre, y=Amount)) + stat_summary(fun.y="sum", geom="point")
于 2011-03-07T09:22:22.123 に答える
1

次のようなことを試してください:

dtf <- structure(list(Genre = structure(c(2L, 3L, 2L, 1L, 2L, 3L), .Label = c("Action", 
"Comedy", "Drama"), class = "factor"), Amount = c(10, 30, 20, 
20, 20, 20)), .Names = c("Genre", "Amount"), row.names = c(NA, 
-6L), class = "data.frame")

library(reshape)
library(ggplot2)
mdtf <- melt(dtf)
cdtf <- cast(mdtf, Genre ~ . , sum)
ggplot(cdtf, aes(Genre, `(all)`)) + geom_bar()
于 2011-03-07T09:18:32.663 に答える