gpt4 book ai didi

r - 如何使用不同的点大小来表示该点所在位置的数量

转载 作者:行者123 更新时间:2023-12-04 09:35:19 24 4
gpt4 key购买 nike

我正在处理分类数据,并试图绘制一个散点图,其中点的大小应表示该点位置的频率。

我首先尝试了抖动,但我对该解决方案不满意。

我以为我可以创建一个频率列,但没有设法为此创建代码。

    qplot(X, Y, data=datatable, geom=c("point"))

有人有想法吗?

谢谢

最佳答案

这是对您所追求的猜测。在 df下面的数据框,xy是您的分类变量。有多种方法可以获取频率计数。在这里,ddply()来自 plyr 的函数包被使用。其次是剧情。在调用 ggplot : size美学确保点的大小代表频率;和 scale_size_discrete()函数控制图上点的大小。

# Some toy data
df <- structure(list(x = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4", "5"
), class = "factor"), y = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3",
"4", "5"), class = "factor")), .Names = c("x", "y"), row.names = c(NA,
79L), class = "data.frame")

# Required packages
library(plyr)
library(ggplot2)

# Get the frequency counts
dfc <- ddply(df, c("x", "y"), "nrow", .drop = FALSE)
#dfc

# The plot
ggplot(data = dfc, aes(x = x, y = y, size = factor(nrow))) +
geom_point() +
scale_size_discrete(range = c(1, 10))

enter image description here

或使用 df 的相同绘图数据框 - 未聚合的数据。
ggplot(data = df, aes(x = x, y = y)) +
stat_sum(aes(size = factor(..n..)), geom = "point") +
scale_size_discrete(range = c(1, 10))

关于r - 如何使用不同的点大小来表示该点所在位置的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com