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

发布于 2025-01-31 18:08:40 字数 939 浏览 2 评论 0原文

我在数据中做了这样的事情,但是尽管透明度很难可视化(我的数据的段数比下面的示例要少得多),以查看它们的开始和结束。

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,但它需要ymaxymax是否可以完全集成供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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱已欠费 2025-02-07 18:08:40

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

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

“在此处输入图像说明”

但似乎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()

“在此处输入图像说明”

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

enter image description here

But it seems geom_linerange allows dodging.

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

enter image description here

你是暖光i 2025-02-07 18:08:40

更新ggplot2版本3.4.2,并在另一个“ https://stackoverflow com”上进行

更新位置= position_dodge2(width = 0.1)
例如:

ggplot(iris, aes(x = Species,
                 ymin = Petal.Length,
                 ymax = Petal.Width)) +
  geom_linerange(position = position_dodge2(width = 0.5)) +
  coord_flip() +
  theme_bw()

将产生这个数字:

​> 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:

ggplot(iris, aes(x = Species,
                 ymin = Petal.Length,
                 ymax = Petal.Width)) +
  geom_linerange(position = position_dodge2(width = 0.5)) +
  coord_flip() +
  theme_bw()

Will produce this figure:Example ggplot2 output.

Note that ggplot2 only allows geom_linerange() for the y-axis, hence the use of coord_flip().

情绪少女 2025-02-07 18:08:40

位置 geom_seggment的参数可能对您有帮助,请参阅稍微更改Geom_Segment的位置,但要保持XEND CONSTAND的位置

The position argument of geom_segment might be helpful for you, see Slightly change geom_segment's position of x only, but keep position of xend constant

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文