[英]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.