如何抖动/躲避geom_segments使它们保持平行?

Ant*_*nto 11 plot r ggplot2

我用我的数据做了类似的事情,但是尽管透明度很高,但很难看到这些片段(我的数据的片段数比下面的例子少得多)才能看到它们的开头和结尾.

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))
Run Code Online (Sandbox Code Playgroud)

碰到这个解决方案,但线条交叉.有没有办法让抖动产生与尖端点的平行线?我试过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))
Run Code Online (Sandbox Code Playgroud)

San*_*att 10

据我所知,geom_segment不允许抖动或躲闪.您可以将抖动添加到数据框中的相关变量,然后绘制抖动变量.在您的示例中,因子将转换为数字,然后使用因子将因子级别的标签添加到轴scale_y_continuous.

library(ggplot2)
iris$JitterSpecies <- ave(as.numeric(iris$Species), iris$Species, 
   FUN = function(x) x + rnorm(length(x), sd = .1))

ggplot(iris, aes(x = Petal.Length, xend = Petal.Width,
                 y = JitterSpecies, yend = JitterSpecies)) +
    geom_segment()+
    geom_point(aes(size=Sepal.Length, shape=Species)) +
    scale_y_continuous("Species", breaks = c(1,2,3), labels = levels(iris$Species))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但它似乎geom_linerange允许躲避.

ggplot(iris, aes(y = Petal.Length, ymin = Petal.Width,
                 x = Species, ymax = Petal.Length, group = row.names(iris))) +
       geom_point(position = position_dodge(.5)) +
     geom_linerange(position = position_dodge(.5)) +
     coord_flip()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @Sandy Muspratt你知道关于`geom_segment`的躲避行为的任何文档吗(“据我所知,`geom_segment`不允许抖动或躲避”)?我看过[**这篇文章**](https://groups.google.com/forum/#!searchin/ggplot2/geom_segment$20position_dodge/ggplot2/iWojkpMicRY/ZMR75OOpHBQJ)关于_vertical_躲避,但我有类似的问题对于水平躲避:`y` 被躲避,但 `yend` 不被躲避。有点令人费解的是“geom_segment”有一个“position”参数,如果它无论如何都不起作用...... (5认同)