绘制摘要统计数据

时间:2011-03-07 08:53:22

标签: r ggplot2 summarization

对于以下数据集,

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

我想构建一个ggplot2折线图,其中x轴是Genre,y轴是所有金额的总和(以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()

但无济于事。

2 个答案:

答案 0 :(得分: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

您可以使用qplotstat="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")

答案 1 :(得分: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()