R 有条件地使用ggplot2中的抖动(带有geom_点)

R 有条件地使用ggplot2中的抖动(带有geom_点),r,plot,ggplot2,visualization,R,Plot,Ggplot2,Visualization,我有一个12个变量的图,分为两组。我不能使用刻面,但使用颜色和形状,我能够使可视化更容易理解。但是,也有一些重叠点(部分或全部)。我使用抖动来处理这些问题,但正如您从附件中看到的,这会导致所有点都被移动,而不仅仅是那些重叠的点。 有没有办法有条件地使用抖动或减淡?更好的是,有没有办法将部分重叠的点并排放置?如您所见,我的x轴是离散类别,稍微向左/向右移动并不重要。我尝试将点绘图与binaxis='y'结合使用,但这完全破坏了x轴 编辑:已经成功地完成了我正在搜索的内容 进一步编辑:添加此可视化

我有一个12个变量的图,分为两组。我不能使用刻面,但使用颜色和形状,我能够使可视化更容易理解。但是,也有一些重叠点(部分或全部)。我使用抖动来处理这些问题,但正如您从附件中看到的,这会导致所有点都被移动,而不仅仅是那些重叠的点。

有没有办法有条件地使用抖动或减淡?更好的是,有没有办法将部分重叠的点并排放置?如您所见,我的x轴是离散类别,稍微向左/向右移动并不重要。我尝试将点绘图与
binaxis='y'
结合使用,但这完全破坏了x轴

编辑:已经成功地完成了我正在搜索的内容

进一步编辑:添加此可视化背后的代码

disciplines <- c("Comp. Sc.\n(17.2%)", "Physics\n(19.6%)", "Maths\n(29.4%)", "Pol.Sc.\n(40.4%)", "Psychology\n(69.8%)")

# To stop ggplot from imposing alphabetical ordering on x-axis
disciplines <- factor(disciplines, levels=disciplines, ordered=T)

# involved aspects
intensive   <- c( 0.660,  0.438,  0.515,  0.028,  0.443)
comparative <- c( 0.361,  0.928,  0.270,  0.285,  0.311)
wh_adverbs  <- c( 0.431,  0.454,  0.069,  0.330,  0.577)
past_tense    <- c(0.334, 0.229, 0.668, 0.566, 0.838)
present_tense <- c(0.680, 0.408, 0.432, 0.009, 0.996)
conjunctions <- c( 0.928,  0.207,  0.162, -0.299, -0.045)
personal      <- c(0.498, 0.521, 0.332, 0.01, 0.01)
interrogative <- c(0.266, 0.202, 0.236, 0.02, 0.02)
sbj_objective <- c(0.913, 0.755, 0.863, 0.803, 0.913)
possessive    <- c(0.896, 0.802, 0.960, 0.611, 0.994)
thrd_person <- c(-0.244, -0.265, -0.310, -0.008, -0.384)
nouns       <- c(-0.602, -0.519, -0.388, -0.244, -0.196)

df1 <- data.frame(disciplines,
                 "Intensive Adverbs"=intensive,
                 "Comparative Adverbs"=comparative,
                 "Wh-adverbs (WRB)"=wh_adverbs,
                 "Verb: Past Tense"=past_tense,
                 "Verb: Present Tense"=present_tense,
                 "Conjunctions"=conjunctions,
                 "Personal Pronouns"=personal,
                 "Interrogative Pronouns"=interrogative,
                 "Subjective/Objective Pronouns"=sbj_objective,
                 "Possessive Pronouns"=possessive,
                 "3rd-person verbs"=thrd_person,
                 "Nouns"=nouns,
                 check.names=F)

df1.m <- melt(df1)
grp <- ifelse(df1.m$variable %in% c('3rd-person verbs','Nouns'), 'Informational Features', 'Involved Features')
g <- ggplot(df1.m, aes(group=grp, disciplines, value, shape=grp, colour=variable))
g <- g + geom_hline(yintercept=0, size=9, color="white")
g <- g + geom_smooth(method=loess, span=0.75, level=0.95, alpha=I(0.16), linetype="dashed")
g <- g + geom_point(size=4,  alpha=I(0.7), position=position_jitter(width=0.1, height=0))
g <- g + scale_shape_manual(values=c(17,19))

规程我很好奇其他人可能会提出什么建议,但为了获得副作用,您可以根据您用于颜色的类别,将x轴长轴类别编码为数字(10,20,…50)加/减少量(0..10)/2。所以你可以得到x轴为9.6,9.8,10.0,10.2。。。然后是20.0,20.2,20.4。这可以创建一个有组织的绘图,而不是随机分配这些分数调整

下面是这个想法在您的数据集上的一个快速实现。它将主x变量
偏移子类别变量的六分之一,并将该变量无抖动地用于x值

M = df1.m
ScaleFactor = 6
xadj = as.numeric(M$variable)/ScaleFactor
xadj = xadj - mean(xadj)   # shift it to center around zero
x10  = as.numeric(M$disciplines) * 10
M$x = x10 + xadj
g = ggplot(M, aes(group=grp, x, value, shape=grp, colour=variable)) 
g + geom_point(size=4,alpha=I(0.7)) + scale_x_discrete(breaks=x10,labels=disciplines)
请注意,每个类别中的值以相同的顺序均匀分布。(此代码不包括图中所示的所有曲线拟合等)

变化:如果您“量化”y值,您可以更清楚地看到效果,因此更多的y值会并排打印

M$valmod = M$value - M$value %% 0.2 + .1
然后在
aes()
语句中使用
valmod
代替
value
,以查看效果

要取回类别标签,请手动设置为
scale\u x\u discrete
。此版本使用不同的
ScaleFactor
以获得更宽的间距和量化的y轴:

M=df1.m
ScaleFactor = 3
# Note this could just be xadj instead of adding to data frame
M$xadj = as.numeric(M$variable)/ScaleFactor
M$xadj = M$xadj - mean(M$xadj)   # shift it to center around zero
M$x10  = as.numeric(M$disciplines) * 10
M$x = M$x10 + M$xadj

Qfact = 0.2  # resolution to quantize y values
M$valmod = M$value - M$value %% Qfact + Qfact/2  # clump y to given resolution

g = ggplot(M, aes(group=grp, x, valmod, shape=grp, colour=variable)) +
    scale_x_discrete(limits = M$x10, breaks=unique(M$x10),labels=levels(M$disciplines))
g + geom_point(size=3,alpha=I(0.7))

您应该提供一个可复制的示例(数据+代码),让其他人使用它…添加代码。希望这有帮助:-)谢谢你的代码。另外,你的绘图看起来不像biomed示例那样干净,因为你的Y值到处都是,但是你仍然可以按照下面的顺序排列x值。我在抖动中使用了
height=0
。所有点的垂直位置直接来自数据。我只是不喜欢水平位置如何移动,即使附近没有其他数据点。这看起来很棒!但是有没有办法取回x轴上的原始类别名称而不是数值?是的,这是“读者的练习”,但我在。。。我希望看到这个“均匀抖动”添加到基本ggplot2中。也许一些R大师也会有另一种方法。