gpt4 book ai didi

r - 使用 geom_segment 按因素分组数据

转载 作者:行者123 更新时间:2023-12-04 16:31:21 26 4
gpt4 key购买 nike

我想使用 geom_segment 按因子组显示段簇,但 position 参数似乎没有任何作用。这是一个例子:

mydata = data.frame(variable = factor(c("A","A","A","B","C")),
color = factor(c(1,2,3,4,5)),
start = c(1,2,1,4,6),
end = c(3,4,6,5,8))

ggplot(mydata, aes(x = start, xend = end, y = variable, yend = variable)) +
geom_segment(aes(color = color, position = "stack"), size = 3)

enter image description here

我也尝试过 position = "dodge"。 A 组应该有 3 个部分,但它们都被绿色条遮住了。调整透明度会在视觉上过于困惑。如何确保每个因素的所有分割都并排显示?

最佳答案

我有点不确定@alistaire 和我是否清楚地向您传达了这一点,所以我们的意思是:

mydata = data.frame(variable = factor(c("A","A","A","B","C")),
color = factor(c(1,2,3,4,5)),
start = c(1,2,1,4,6),
end = c(3,4,6,5,8))

ggplot(mydata, aes(ymin = start, ymax = end, x = variable)) +
geom_linerange(aes(color = color),position = position_dodge(width = 0.2), size = 3) +
coord_flip()

结果是:

enter image description here

关于r - 使用 geom_segment 按因素分组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322919/

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