填充 geom_freqpoly 线下区域的最简单方法是什么?

发布于 2024-08-31 05:23:09 字数 1058 浏览 0 评论 0原文

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

plots

我希望有一个填充区域,例如由 提供的geom_密度,但没有平滑线条:

smoooth

已建议使用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))

产生:

desired

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2024-09-07 05:23:09

也许你想要:

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

Perhaps you want:

geom_area(aes(y = ..count..), stat = "bin")
杀お生予夺 2024-09-07 05:23:09

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.

忆伤 2024-09-07 05:23:09

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.

浅沫记忆 2024-09-07 05:23:09

我不完全确定你的目标是什么。你想要一条线还是一条条。您应该查看 geom_bar 来查看填充条。比如:

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

如果你想在下面填充一条线,那么你应该看看 geom_area 其中我个人没有使用过,但看起来结构几乎是一样的。

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

希望有帮助。提供更多信息,我们可能会提供更多帮助。

实际上我会添加一个索引,只是数据行并将其用作 x,然后使用

p <- ggplot(data, aes(x = index, y = count))
p + geom_bar(stat = "identity") + scale_x_continuous("Intervals", 
breaks = index, labels = intervals)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文