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

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

问题描述

我用我的数据做了类似的事情,但尽管透明度很高,但看起来很难直观(我的数据的段数比下面的例子少得多),以查看它们的开始和结束。

  require(ggplot2)
ggplot(iris,aes(x = Petal.Length,xend = Petal.Width,
y = factor (aes(形状=物种))
()

来到这个解决方案,但是线条是交叉的。有没有办法让抖动与尖端的点产生平行线?我试过 position_dodge 而不是 position_jitter ,但它需要 ymax 。根据 geom_segment



可以< ymax pre> 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 将因子水平的标签添加到轴。

  library(ggplot2)
iris $ JitterSpecies < - ave(as.numeric(iris $ Species),iris $物种,
FUN =函数(x)x + rnorm(长度(x),sd = .1))

ggplot(虹膜,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()


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

解决方案

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

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

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

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