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()
Run Code Online (Sandbox Code Playgroud)
position_dodge由于数据在该轴上过于拥挤,因此无法使用水平躲避,因此某些点仍然重叠并且可视化效果不清晰。
p+geom_point(position=position_dodge(width=1))+
ggtitle('position_dodge isnt what Im looking for.
\nx-axis too crowded and points still overlap')
Run Code Online (Sandbox Code Playgroud)
position_jitter这种工作方式是因为我可以将x抖动限制为0,并控制y抖动的程度。但是,抖动的随机性使其吸引力降低。当它们存在时,我可以区分出三种颜色。
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')
Run Code Online (Sandbox Code Playgroud)
感谢@aosmith的建议ggstance::position_dodgev()。这正是我想要的。我增加了过采样,因此效果更加明显。
df <- expand.grid(
y_factor = paste0('factor_',1:5),
x =1:100
)%>%as.tbl
seed<-1
df_with_overlap<-df%>%
sample_frac(1.5,replace = TRUE)%>%
group_by(y_factor,x)%>%
mutate(n=factor(1:n()))
ggplot(data=df_with_overlap, aes(x=x, y=y_factor, col=n))+
geom_point(position=ggstance::position_dodgev(height=0.3))
Run Code Online (Sandbox Code Playgroud)