gpt4 book ai didi

r - 不重叠的抖动点

转载 作者:行者123 更新时间:2023-12-05 08:55:52 24 4
gpt4 key购买 nike

我的数据:

a <- sample(1:5, 100, replace = TRUE)
b <- sample(1:5, 100, replace = TRUE)
c <- sample(1:10, 100, replace = TRUE)
d <- sample(1:40, 100, replace = TRUE)
df <- data.frame(a, b, c, d)

使用 ggplot2,我在 x = a 和 y = b 上创建了散点图,在二维中加权(通过 colour = csize = d)。请注意,x 和 y 是有意设置的 1:5

很明显,不同大小和颜色的点因此重叠,所以我尝试抖动避免重叠:

ggplot(df, aes(a, b, colour = c, size = d)) + 
geom_point(position = position_jitter())

现在我希望这些点聚集得更近,所以我尝试了几个heightwidth的组合,用于抖动功能,如

ggplot(df, aes(a, b, colour = c, size = d)) + 
geom_point(position = position_jitter(width = 0.2, height = 0.2))

抖动使点仍然重叠,并将它们随机分布在给定区域上。

有没有办法让点完全不重叠,但尽可能靠近聚集在一起,甚至可能接触而不是“并排”或堆叠? (在某种程度上,用更小的点创造一种气泡)?

谢谢!

最佳答案

根据@Tjebo 的建议,我在“堆”中排列了点。

set.seed(1234)
n <- 100
a <- sample(1:5,n,rep=TRUE)
b <- sample(1:5,n,rep=TRUE)
c <- sample(1:10,n,rep=TRUE)
d <- sample(1:40,n,rep=TRUE)
df0 <- data.frame(a,b,c,d)

# These parameters need carefully tuning
minr <- 0.05
maxr <- 0.2
# Order circles by dimension
ord <- FALSE

df1 <- df0
df1$d <- minr+(maxr-minr)*(df1$d-min(df1$d))/(max(df1$d)-min(df1$d))
avals <- unique(df1$a)
bvals <- unique(df1$b)

for (k1 in seq_along(avals)) {
for (k2 in seq_along(bvals)) {
print(paste(k1,k2))
subk <- (df1$a==avals[k1] & df1$b==bvals[k2])
if (sum(subk)>1) {
subdfk <- df1[subk,]
if (ord) {
idx <- order(subdfk$d)
subdfk <- subdfk[idx,]
}
subdfk.mod <- subdfk
posmx <- which.max(subdfk$d)
subdfk1 <- subdfk[posmx,]
subdfk2 <- subdfk[-posmx,]
angsk <- seq(0,2*pi,length.out=nrow(subdfk2)+1)
subdfk2$a <- subdfk2$a+cos(angsk[-length(angsk)])*(subdfk1$d+subdfk2$d)/2
subdfk2$b <- subdfk2$b+sin(angsk[-length(angsk)])*(subdfk1$d+subdfk2$d)/2
subdfk.mod[posmx,] <- subdfk1
subdfk.mod[-posmx,] <- subdfk2
df1[subk,] <- subdfk.mod
}
}
}

library(ggplot2)
library(ggforce)
ggplot(df1, aes()) +
geom_circle(aes(x0=a, y0=b, r=d/2, fill=c), alpha=0.7)+ coord_fixed()

enter image description here

关于r - 不重叠的抖动点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698290/

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