填充 geom_freqpoly 线下区域的最简单方法是什么?
x 轴是将时间分解为时间间隔。数据框中有一个间隔列,用于指定每行的时间。该列是一个因子,其中每个区间是不同的因子级别。
使用 geom_histogram 和 geom_freqpoly 绘制直方图或线条效果很好,但我想要一条线,就像 geom_freqpoly 提供的那样,并填充区域。
目前我正在使用 geom_freqpoly,如下所示:
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))
我希望有一个填充区域,例如由 提供的geom_密度
,但没有平滑线条:
已建议使用geom_area
,有没有办法使用 ggplot2 生成的统计数据,例如 ..count..,作为 geom_area 的 y 值?或者,在使用 ggplot2 之前是否需要进行计数聚合?
正如答案中所述, geom_area(..., stat = "bin") 是解决方案:
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))
产生:
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))
I would prefer to have a filled area, such as provided by geom_density
, but without smoothing the 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许你想要:
Perhaps you want:
geom_ribbon 可用于在两条线之间生成填充区域,而无需显式构造多边形。 这里有很好的文档。
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_密度()
但我不认为这是一个有意义的图形。
ggplot(quake.data, aes(interval, fill=tweet.type, group = 1)) + geom_density()
But I don't think this is a meaningful graphic.
我不完全确定你的目标是什么。你想要一条线还是一条条。您应该查看 geom_bar 来查看填充条。比如:
如果你想在下面填充一条线,那么你应该看看 geom_area 其中我个人没有使用过,但看起来结构几乎是一样的。
希望有帮助。提供更多信息,我们可能会提供更多帮助。
实际上我会添加一个索引,只是数据行并将其用作 x,然后使用
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:
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.
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