在ggplot2图表中按因子计数

发布于 2024-08-07 10:13:05 字数 1026 浏览 1 评论 0原文

给定以下 ggplot2 图表:

ggplot(my_data, aes(colour=my_factor) +   
                geom_point(aes(x=prior, y=current)) +   
                facet_grid(gender ~ age)

我想让点的大小与先前/当前组合的 my_factor 计数成正比。

ggplot(my_data, aes(colour=my_factor, 
                size=<something-here>(my_factor)) +   
                geom_point(aes(x=prior, y=current)) + 
                facet_grid(gender ~ age)

有什么想法吗?

== 编辑 ==

这是一个基于 mpg 数据集的非常简单的示例。让我们将“great_hwy”定义为 hwy > 35,“great_cty”为 cty > 25:

mpg$great_hwy[mpg$hwy > 35]  <-1
mpg$great_hwy[mpg$hwy <= 35] <-0
mpg$great_hwy <- factor(mpg$great_hwy)

mpg$great_cty[mpg$cty > 25]  <- 1
mpg$great_cty[mpg$cty <= 25] <- 0
mpg$great_cty <- factor(mpg$great_cty)

如果我们绘制great_hwy 与great_cty 的图,它不会告诉我们太多信息:

ggplot(mpg) + geom_point(aes(x=great_cty, y=great_hwy))

如何根据x/y 点的数量使数据点的大小更大?希望这能解决问题,但否则请告诉我。

Given the following ggplot2 chart:

ggplot(my_data, aes(colour=my_factor) +   
                geom_point(aes(x=prior, y=current)) +   
                facet_grid(gender ~ age)

I would like to make the size of the points be proportional to the count of my_factor for that prior/current combination.

ggplot(my_data, aes(colour=my_factor, 
                size=<something-here>(my_factor)) +   
                geom_point(aes(x=prior, y=current)) + 
                facet_grid(gender ~ age)

Any ideas?

== Edit ==

Here's a very trivial example based on mpg dataset. Let's define "great_hwy" as hwy > 35, and "great_cty" as cty > 25:

mpg$great_hwy[mpg$hwy > 35]  <-1
mpg$great_hwy[mpg$hwy <= 35] <-0
mpg$great_hwy <- factor(mpg$great_hwy)

mpg$great_cty[mpg$cty > 25]  <- 1
mpg$great_cty[mpg$cty <= 25] <- 0
mpg$great_cty <- factor(mpg$great_cty)

If we plot great_hwy vs. great_cty, it won't tell us much:

ggplot(mpg) + geom_point(aes(x=great_cty, y=great_hwy))

How could I make the data points bigger in size depending on the number of x/y points? Hope this clears it up, but let me know otherwise.

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

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

发布评论

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

评论(2

嗼ふ静 2024-08-14 10:13:05

您当然可以通过在 ggplot 外部进行计数来做到这一点,但 ggplot 的一大优点是您可以在内部进行许多此类统计!

使用上面的 mpg 示例:

ggplot(mpg) + 
  geom_point(aes(x=great_cty, y=great_hwy, 
                 size=..count..), stat="bin")

alt text

You can certainly do this by counting external to ggplot, but one of the great things about ggplot is that you can do many of these statistics internally!

Using your mpg example above:

ggplot(mpg) + 
  geom_point(aes(x=great_cty, y=great_hwy, 
                 size=..count..), stat="bin")

alt text

成熟稳重的好男人 2024-08-14 10:13:05

因为接受的答案使用了已弃用的功能,所以我将指出适用于 ggplot2 1.0.1 的替代答案

ggplot2 可视化绘制在彼此之上的点的计数:stat_bin2d 或 geom_tile 或点大小?

Because the accepted answer uses a deprecated feature I'll point out this alternate answer that works for ggplot2 1.0.1

ggplot2 visualizing counts of points plotted on top of each other: stat_bin2d or geom_tile or point size?

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