How to make geom_path correspond to geom_beeswarm points

253 Views Asked by At

I've been trying to visualize the association of several characteristics in a small sample.

Here is an example data:

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

I tried it with geom_point like this:

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

example 1 with geom_point

The problem is the points are overlapped no matter what width I have in the position_dodge(). geom_beeswarm produces a better looking plot:

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

example 2 with geom_beeswarm

But the connection with geom_path falls apart. The lines are supposed to connect the records with the same ID.

Is there a way to make geom_path correspond to geom_beeswarm points? Or, is there a way to tweak geom_point to have equally spaced points like geom_beeswarm?

0

There are 0 best solutions below