我一直试图在一个小样本中可视化几个特征的关联。
这是一个示例数据:
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()
问题是无论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_path
分崩离析。这些行应该连接具有相同 ID 的记录。
有没有办法使geom_path
对应geom_beeswarm
点?或者,有没有办法调整geom_point
到等间距的点,比如geom_beeswarm
?