我想使用因子组显示段集群,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)
Run Code Online (Sandbox Code Playgroud)
我也试过了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()
Run Code Online (Sandbox Code Playgroud)
结果是: