For the following data set,
Genre Amount
Comedy 10
Drama 30
Comedy 20
Action 20
Comedy 20
Drama 20
I want to construct a ggplot2 line graph, where the x-axis is Genre and the y-axis is the sum of all amounts (conditional on the Genre).
I have tried the following:
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()
but to no avail.
If you don’t want to compute a new data frame before plotting, you cvan use
stat_summaryin ggplot2. For example, if your data set looks like this :You can use either
qplotwith astat="summary"argument :Or add a
stat_summaryto a baseggplotgraphic :