I would like to dodge overlapping geom_point's vertically, when I have a categorical y variable.
library(tidyverse)
# all possible points
df <- expand.grid(
y_factor = paste0('factor_',1:5),
x =1:100
)%>%as.tbl
# randomly missing and overlapping points
# every green point has a pink point underneath, and every blue point
# has a green point underneath it.
seed<-1
df_with_overlap<-df%>%
sample_frac(0.5,replace = TRUE)%>%
group_by(y_factor,x)%>%
mutate(n=factor(1:n()))
p<-ggplot(data=df_with_overlap, aes(x=x, y=y_factor, col=n))
p+geom_point()
Dodging horizontally using position_dodge
doesn't work because the data is too crowded on that axis, so some points still overlap and the visualization isn't clear.
p+geom_point(position=position_dodge(width=1))+
ggtitle('position_dodge isnt what Im looking for.
\nx-axis too crowded and points still overlap')
position_jitter
kind of works because I can limit x jitter to 0, and control the degree of y jitter. But the randomness of the jitter makes it less appealing. I can kind of make out the 3 colours when they exist.
p+geom_point(aes(col=n), position=position_jitter(width=0, height=0.05))+
ggtitle('Jitter kind of works.
\nIt would work better if it wasnt random
\nlike position_dodge, but vertical dodging')
Is there a way to dodge the points vertically?