我想使用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)
我也试过position = "dodge"
。 A组应该有3个部分,但它们都被绿色条所掩盖。调整透明度在视觉上会太混乱。如何确保每个因素的所有段都并排显示?
答案 0 :(得分:4)
我有点不确定@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()
结果是: