r - 如何抖动/闪避geom_segments以便它们保持平行?

标签 r plot 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))


遇到了this解决方案,但是这些线是交叉的。有没有办法使抖动产生与尖端的平行线?我尝试使用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))

最佳答案

据我所知,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))




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

关于r - 如何抖动/闪避geom_segments以便它们保持平行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904364/

相关文章:

javascript - R plotly : How to use config() in conjunction with plotly_build()?

r - 使用 ggplot2 绘制 POSIXct 时间戳系列

r - 如何改变点图的形状?

r - ggplot 轮廓抖动数据点

c++ - 如何使用 RcppParallel 创建 Rcpp::CharacterMatrix 的线程安全包装?

matlab - 在 MATLAB 中绘制体积数据

r - 有效地创建 a x b 扩展网格内存

python - pyqtgraph 中填充图的透明度

r - 计算空间计量经济学模型中的 p.values : why are there inconsistencies between summary() and texreg()?

r - 调整ggplot中的特定文本标签以避免重叠