How to use geom_histogram to plot mean value instead of count?

616 views
Skip to first unread message

Tom Song

unread,
Sep 19, 2009, 2:38:15 PM9/19/09
to ggplot2
Hi,

I have data like this:

x y
0.42 23
0.21 14
0.53 12
0.82 17
0.35 22
. .
. .
. .
0.27 12

Is it possible to plot histogram with bin in x variable and the
height of the bin is the mean value of y within that bin?

Thanks for help!

Tom

hadley wickham

unread,
Sep 19, 2009, 2:46:08 PM9/19/09
to Tom Song, ggplot2
Have a look at stat_summary.
Hadley
--
http://had.co.nz/

Tom Song

unread,
Sep 19, 2009, 7:15:29 PM9/19/09
to ggplot2
Hi Hadley,

m <- ggplot(my_dataframe, aes(x, y))
m + stat_summary(fun.y = "mean", geom = "bar")

Thanks!

Regards,

Tom

Tom Song

unread,
Sep 22, 2009, 9:21:11 AM9/22/09
to ggplot2
Hi Hadley,

I found the following command treats each x as individual value
and doesn't bin them. Is it possible to bin x and get a mean value of
y for that bin as bin height?

m <- ggplot(my_dataframe, aes(x, y))
m + stat_summary(fun.y = "mean", geom = "histogram")

Thanks for help!

Regards,

Tao

hadley wickham

unread,
Sep 22, 2009, 9:24:40 PM9/22/09
to Tom Song, ggplot2
Hi Tom,

Oh good point. In that case it's probably best to do it by hand:

diamonds$carat_bin <- cut_interval(diamonds$carat, length = 0.1)
carat_sum <- ddply(diamonds, "carat_bin", summarise,
mean_price = mean(price, na.rm = T),
mid = mean(range(carat, na.rm = T)),
)

qplot(mid, mean_price, data = carat_sum, geom = "bar", width = 0.1,
stat = "identity", position = "identity")

(this isn't quite right, but it's pretty close)

Hadley
--
http://had.co.nz/

tao song

unread,
Sep 22, 2009, 10:27:09 PM9/22/09
to hadley wickham, ggplot2
Hi Hadley,

    It would be great if in the future stat_summary can bin x value if it is continuous.

    Thanks for your great work!

    Regards,
 
    Tom
Reply all
Reply to author
Forward
0 new messages