Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting the average values for each level in ggplot2

Tags:

r

ggplot2

I'm using ggplot2 and am trying to generate a plot which shows the following data.

df=data.frame(score=c(4,2,3,5,7,6,5,6,4,2,3,5,4,8),               age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16)) str(df) df 

Instead of doing a frequency plot of the variables (see below code), I want to generate a plot of the average values for each x value. So I want to plot the average score at each age level. At age 18 on the x axis, we might have a 3 on the y axis for score. At age 23, we might have an average score of 4.5, and so forth (Edit: average values corrected). This would ideally be represented with a barplot.

ggplot(df, aes(x=factor(age), y=factor(score))) + geom_bar() Error: stat_count() must not be used with a y aesthetic. 

Just not sure how to do this in R with ggplot2 and can't seem to find anything on such plots. Statisticially, I don't know if the plot I desire to plot is even the right thing to do, but that's a different store.

Thanks!

like image 552
ATMathew Avatar asked Aug 08 '12 05:08

ATMathew


People also ask

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

Is ggplot2 better than Matplotlib?

Both packages achieved very similar results. But the contour lines, labels, and legend in matplotlib are superior to ggplot2.


2 Answers

You can use summary functions in ggplot. Here are two ways of achieving the same result:

# Option 1 ggplot(df, aes(x = factor(age), y = score)) +    geom_bar(stat = "summary", fun = "mean")  # Option 2 ggplot(df, aes(x = factor(age), y = score)) +    stat_summary(fun = "mean", geom = "bar") 

enter image description here

Older versions of ggplot use fun.y instead of fun:

ggplot(df, aes(x = factor(age), y = score)) +    stat_summary(fun.y = "mean", geom = "bar") 
like image 157
DrDom Avatar answered Sep 22 '22 02:09

DrDom


If I understood you right, you could try something like this:

library(plyr) library(ggplot2) ggplot(ddply(df, .(age), mean), aes(x=factor(age), y=factor(score))) + geom_bar() 
like image 31
johannes Avatar answered Sep 25 '22 02:09

johannes