tags:

views:

100

answers:

3

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))

alt text

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

UPDATE:

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?

UPDATE 2:

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

This:

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: alt text

+1  A: 

I'm not entirely sure what you're aiming for. Do you want a line or bars. You should check out geom_bar for filled bars. Something like:

p <- ggplot(data, aes(x = time, y = count))
p + geom_bar(stat = "identity")

If you want a line filled in underneath then you should look at geom_area which I haven't personally used but it appears the construct will be almost the same.

p <- ggplot(data, aes(x = time, y = count))
p + geom_area()

Hope that helps. Give some more info and we can probably be more helpful.

Actually i would throw on an index, just the row of the data and use that as x, and then use

p <- ggplot(data, aes(x = index, y = count))
p + geom_bar(stat = "identity") + scale_x_continuous("Intervals", 
breaks = index, labels = intervals)
Dan
and the scale works for geom_area and geom_bar
Dan
+1  A: 

ggplot(quake.data, aes(interval, fill=tweet.type, group = 1)) + geom_density()

But I don't think this is a meaningful graphic.

hadley
After seeing it, I agree. I'll update my question to clarify that I'm looking for a geom_freqpoly plot with a filled area. It looks like geom_area would work, but is it possible to use ggplot2 provided statistics (e.g., ..count..) for y-axis values?
mattrepl
+1  A: 

Perhaps you want:

geom_area(aes(y = ..count..), stat = "bin")

?

hadley
Thank you, I've used 'stat = "bin"' before but forgot about it. I need to buy your book. =)
mattrepl