抖动点没有重叠

时间:2017-04-29 17:00:17

标签: r plot ggplot2 overlap scatter

我的数据:

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

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

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

谢谢!

3 个答案:

答案 0 :(得分:4)

一个有趣的可视化工具是 beeswarm 图 在R中,beeswarmggbeeswarm包实现了这种情节。

以下是ggbeeswarm的示例:

set.seed(1234)
a <- sample(1:5,100,rep=TRUE)
b <- sample(1:5,100,rep=TRUE)
c <- sample(1:10,100,rep=TRUE)
d <- sample(1:40,100,rep=TRUE)
df <- data.frame(a,b,c,d)
library(ggbeeswarm)
ggplot(aes(x=a, y=b, col=c, size=d),  data = df)+
  geom_beeswarm(priority='random',cex=3.5, groupOnX=T)+coord_flip()

enter image description here我希望这可以帮到你。

答案 1 :(得分:3)

根据@ 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

答案 2 :(得分:0)

这是@Tjebo抖动问题的另一种可能的解决方案 参数dst需要进行一些调整。

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

dst <- .2

df.mod <- df
avals <- unique(df$a)
bvals <- unique(df$b)
for (k1 in seq_along(avals)) {
  for (k2 in seq_along(bvals)) {
    subk <- (df$a==avals[k1] & df$b==bvals[k2])
    if (sum(subk)>1) {
      subdf <- df[subk,]
      angsk <- seq(0,2*pi,length.out=nrow(subdf)+1)
      ak <- subdf$a+cos(angsk[-1])*dst
      bk <- subdf$b+sin(angsk[-1])*dst
      df.mod[subk,c("a","b")] <- cbind(ak,bk)
    }
  }
}

library(ggplot2)
ggplot(df.mod, aes(a, b, colour = c, size = d)) + geom_point()

enter image description here