I have data frame (df) with 3 columns e.g.
NUMERIC1: NUMERIC2: GROUP(CHARACTER):
100 1 A
200 2 B
300 3 C
400 4 A
I want to group NUMERIC1 by GROUP(CHARACTER), and then calculate mean for each group. Something like that:
mean(NUMERIC1): GROUP(CHARACTER):
250 A
200 B
300 C
Finally I'd like to draw bar chart using ggplot2 having GROUP(CHARACTER) on x axis a =nd mean(NUMERIC) on y axis. It should look like:
I used
mean <- tapply(df$NUMERIC1, df$GROUP(CHARACTER), FUN=mean)
but I'm not sure if it's ok, and even if it's, I don't know what I supposed to do next.
This is what
stat_summmary(...)
is designed for:Try something like:
data
Here's a solution using
dplyr
to create the summary. In this case, the summary is created on the fly withinggplot
, but you can also create a separate summary data frame first and then feed that toggplot
.Since you're plotting means, rather than counts, it might make more sense use points, rather than bars. For example:
I'd suggest something like:
Why ggplot when you could do the same with your own code and barplot: