如何抖动/躲避geom_segments保持平行?
我在数据中做了这样的事情,但是尽管透明度很难可视化(我的数据的段数比下面的示例要少得多),以查看它们的开始和结束。
require(ggplot2)
ggplot(iris, aes(x = Petal.Length, xend = Petal.Width,
y = factor(Species), yend = factor(Species),
size = Sepal.Length)) +
geom_segment(alpha = 0.05) +
geom_point(aes(shape = Species))
遇到 this 解决方案,但是线条是criss-cross。有没有一种方法可以使抖动产生与尖端的点相差线?我尝试过position_dodge
而不是position_jitter
,但它需要ymax
。 ymax
是否可以完全集成供geom_segment
使用?
ggplot(iris, aes(x = Petal.Length, xend = Petal.Width,
y = factor(Species), yend = factor(Species))) +
geom_segment(position = position_jitter(height = 0.25))+
geom_point(aes(size = Sepal.Length, shape = Species))
I did something like this with my data, but despite the transparency the segments are hard to visualise (my data has lot less number of segments than the example below) to see their beginning and end.
require(ggplot2)
ggplot(iris, aes(x = Petal.Length, xend = Petal.Width,
y = factor(Species), yend = factor(Species),
size = Sepal.Length)) +
geom_segment(alpha = 0.05) +
geom_point(aes(shape = Species))
Came across this solution, but the lines are criss-crossed. Is there a way to make the jitter produce parallell lines with the points at the tips? I have tried position_dodge
instead of position_jitter
, but it requires ymax
. Can ymax
be integrated at all for use with geom_segment
?
ggplot(iris, aes(x = Petal.Length, xend = Petal.Width,
y = factor(Species), yend = factor(Species))) +
geom_segment(position = position_jitter(height = 0.25))+
geom_point(aes(size = Sepal.Length, shape = Species))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,
geom_segment
不允许抖动或躲避。您可以在数据框架中的相关变量中添加抖动,然后绘制抖动的变量。在您的示例中,将因子转换为数字,然后使用scale_y_continuul
将因子级别的标签添加到轴上。但似乎
geom_linerange
允许躲避。As far as I know,
geom_segment
does not allow jittering nor dodging. You can add jittering to the relevant variable in the data frame, then plot the jittered variable. In your example, the factor is converted to numeric, then the labels for the levels of the factor are added to the axis usingscale_y_continuous
.But it seems
geom_linerange
allows dodging.更新
ggplot2
版本3.4.2,并在另一个“ https://stackoverflow com”上进行更新
位置= position_dodge2(width = 0.1)
。例如:
将产生这个数字:
> coord_flip()。
Updated for
ggplot2
version 3.4.2 and to build upon another short answer:You may now use
position = position_dodge2(width = 0.1)
.For example:
Will produce this figure:
Note that
ggplot2
only allowsgeom_linerange()
for the y-axis, hence the use ofcoord_flip()
.位置
geom_seggment
的参数可能对您有帮助,请参阅稍微更改Geom_Segment的位置,但要保持XEND CONSTAND的位置The
position
argument ofgeom_segment
might be helpful for you, see Slightly change geom_segment's position of x only, but keep position of xend constant