Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest method to fill the area under a geom_freqpoly line?

Tags:

r

ggplot2

The x-axis is time broken up into time intervals. There is an interval column in the data frame that specifies the time for each row. The column is a factor, where each interval is a different factor level.

Plotting a histogram or line using geom_histogram and geom_freqpoly works great, but I'd like to have a line, like that provided by geom_freqpoly, with the area filled.

Currently I'm using geom_freqpoly like this:

ggplot(quake.data, aes(interval, fill=tweet.type)) + geom_freqpoly(aes(group = tweet.type, colour = tweet.type)) + opts(axis.text.x=theme_text(angle=-60, hjust=0, size = 6))

plots

I would prefer to have a filled area, such as provided by geom_density, but without smoothing the line:

smoooth

The geom_area has been suggested, is there any way to use a ggplot2-generated statistic, such as ..count.., for the geom_area's y-values? Or, does the count aggregation need to occur prior to using ggplot2?


As stated in the answer, geom_area(..., stat = "bin") is the solution:

ggplot(quake.data, aes(interval)) + geom_area(aes(y = ..count.., fill = tweet.type, group = tweet.type), stat = "bin") + opts(axis.text.x=theme_text(angle=-60, hjust=0, size = 6))

produces:

desired

like image 458
mattrepl Avatar asked May 02 '10 19:05

mattrepl


1 Answers

Perhaps you want:

geom_area(aes(y = ..count..), stat = "bin")
like image 173
hadley Avatar answered Oct 08 '22 03:10

hadley