对于以下数据集,
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 回答
如果您不想在绘图之前计算新数据框,则可以在ggplot2中使用
stat_summary
. 例如,如果您的数据集如下所示:您可以使用
qplot
和stat="summary"
参数:或者将
stat_summary
添加到基础ggplot
图形:尝试这样的事情: