gpt4 book ai didi

r - 来自分组散点图中数据点的ggplot2垂直线

转载 作者:行者123 更新时间:2023-12-03 16:14:54 28 4
gpt4 key购买 nike

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

这是我目前拥有的

without vertical lines

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 版本提供了 reproducible jitter .坏消息是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))

关于r - 来自分组散点图中数据点的ggplot2垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243288/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com