如何抖动/躲避 geom_segments 使它们保持平行? [英] how to jitter/dodge geom_segments so they remain parallel?

查看:12
本文介绍了如何抖动/躲避 geom_segments 使它们保持平行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我的数据做了类似的事情,但是尽管这些段是透明的,但很难可视化(我的数据的段数比下面的示例少得多)以查看它们的开头和结尾.

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

遇到了这个解决方案,但线条是纵横交错的.有没有办法让抖动产生与尖端点的平行线?我尝试过 position_dodge 而不是 position_jitter,但它需要 ymax.ymax 能否与 geom_segment 结合使用?

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

推荐答案

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

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

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

这篇关于如何抖动/躲避 geom_segments 使它们保持平行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆