开发者

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

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

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

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

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

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:

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


Perhaps you want:

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


geom_ribbon can be used to produce a filled area between two lines without needing to explicitly construct a polygon. There is good documentation here.


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜