首页 > 解决方案 > 如何使 geom_path 对应 geom_beeswarm 点

问题描述

我一直试图在一个小样本中可视化几个特征的关联。

这是一个示例数据:

set.seed(309)
dat <- data.frame(id=c(1:9, 5:9, 7:9),
           count=c(sample(1:4,9, prob=c(.5,.3,.2,.1), replace = T),
                   c(1,1,3,2,4), c(1,2,1)),
           strain=c(rep("A", 9), rep("B", 5), rep("C", 3)))
dat$feature <- (dat$id%%2==0)*1 + (dat$id%%2!=0)*0
dat$id <- as.factor(dat$id)
dat$feature <- factor(dat$feature, labels=c("X","Y"))

我这样尝试过geom_point

library(ggplot2)
ggplot(dat, aes(x=strain, y=count,group=id))+ 
  geom_point(aes(col=feature), size=10, position = position_dodge(width = 0.5))+
  geom_path(position = position_dodge(width = 0.5))+  theme_minimal()

示例 1 与 geom_point

问题是无论width我在position_dodge(). geom_beeswarm产生一个更好看的情节:

library(ggbeeswarm)
ggplot(dat, aes(x=strain, y=count,group=id))+ 
  geom_beeswarm(aes(col=feature), size=10, cex=6.5)+
  geom_path(position = position_dodge(width = 0.5))+  theme_minimal()

使用 geom_beeswarm 的示例 2

但与的联系geom_path分崩离析。这些行应该连接具有相同 ID 的记录。

有没有办法使geom_path对应geom_beeswarm点?或者,有没有办法调整geom_point到等间距的点,比如geom_beeswarm

标签: rggplot2beeswarm

解决方案


推荐阅读