繁体   English   中英

ggplot2分组散点图中数据点的垂直线

[英]ggplot2 vertical lines from data points in grouped scatter plot

我想知道如何将各个主题数据点的垂直线连接到水平线。 我的主要问题是x轴已分组并且没有连续变量来指定线的位置。 如果我使线从组中向下延伸(xend),则每个组只得到1条垂直线。

这是我目前拥有的

没有垂直线

ggplot() +
geom_point(data = df, aes(x = Group, y = Number, color = Group), position = "jitter") +
geom_hline(yintercept = 33.25)

如果我加

  geom_segment(data = df, 
           aes(x=Group, 
               xend=Group, 
               y=Number, 
               yend=33.25))

我最后每组只有一条垂直线,而不是源于每个主题

好消息是即将推出的ggplot2版本可提供可重现的抖动 坏消息是geom_segment()不允许抖动,因此请改用geom_linerange()

我们将能够像下面这样指定seed 希望该版本能尽快发布! 同时,您应该按照https://stackoverflow.com/a/21922792/5397672的回答,手动将抖动添加到数据中。


reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-12

library(ggplot2)

set.seed(2)
dat <-  iris[sample(1:nrow(iris), 20),]

ggplot(dat, aes(x=Species, y=Petal.Width)) +
  geom_point(position = position_jitter(height = 0L, seed = 1L)) +
  geom_hline(yintercept=0.75) +
  geom_linerange(aes(x=Species, ymax=Petal.Width, ymin=0.75),
                 position = position_jitter(height = 0L, seed = 1L))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM