gpt4 book ai didi

r - geom_point 在分类尺度上的position_dodge 的垂直等效值

转载 作者:行者123 更新时间:2023-12-02 13:50:26 24 4
gpt4 key购买 nike

当我有一个分类 y 变量时,我想避免垂直重叠的 geom_point。

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

no_dodge

使用 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')

position_dodge

position_jitter 是有效的,因为我可以将 x 抖动限制为 0,并控制 y 抖动程度。但抖动的随机性使其吸引力降低。当这 3 种颜色存在时,我可以辨认出它们。

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

position_jitter

有没有办法垂直躲避点?

最佳答案

感谢@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))

answer

关于r - geom_point 在分类尺度上的position_dodge 的垂直等效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52338137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com