在ggplot2图表中按因子计数
给定以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您当然可以通过在 ggplot 外部进行计数来做到这一点,但 ggplot 的一大优点是您可以在内部进行许多此类统计!
使用上面的 mpg 示例:
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:
因为接受的答案使用了已弃用的功能,所以我将指出适用于 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?