2016-04-20 104 views
2

我找不到如何执行此操作的示例。我试图用geom_line画线,但闪避重叠的元素,它似乎不工作。R ggplot2 geom_line位置闪避

这里是我的数据:

> sa 
    id ep type grp variable value 
1: 1 1 typ1 1  st  1 
2: 1 2 typ1 2  st 60 
3: 1 3 typ1 3  st 120 
4: 1 1 typ2 4  st 20 
5: 1 2 typ2 5  st 60 
6: 2 1 typ1 6  st  1 
7: 2 2 typ1 7  st 80 
8: 2 3 typ1 8  st 170 
9: 2 1 typ2 9  st 10 
10: 2 2 typ2 10  st 60 
11: 2 3 typ2 11  st 120 
12: 1 1 typ1 1  en 50 
13: 1 2 typ1 2  en 100 
14: 1 3 typ1 3  en 150 
15: 1 1 typ2 4  en 40 
16: 1 2 typ2 5  en 100 
17: 2 1 typ1 6  en 40 
18: 2 2 typ1 7  en 150 
19: 2 3 typ1 8  en 200 
20: 2 1 typ2 9  en 50 
21: 2 2 typ2 10  en 90 
22: 2 3 typ2 11  en 190 

这里是typ1和我简单的代码试图逃避重叠值typ2

ggplot(sa,aes(x=value,y=id,group=grp,color=type)) + geom_line(size=6,position="dodge") 

plot I get

这是我所看到的。我如何躲避重叠的酒吧?

+1

也许,如果你做的id因素,闪避将起作用 –

回答

5

我不认为你可以垂直闪避,但另一种选择只是增加一点躲闪自己。例如:

eps = 0.05 

ggplot(sa, aes(x=value, y=ifelse(type=="typ1", id + eps, id - eps), 
       group=grp, color=type)) + 
    geom_line(size=6) + 
    scale_y_continuous(breaks=1:2, limits=c(min(sa$id-eps), max(sa$id+eps))) 

你必须与躲避,轴限制等的量左右玩,得到你想要一个给定的宽高比的样子。

enter image description here

5

你只能水平躲闪,但你可以通过翻转你的x和y的美学和使用coord_flip解决这个问题:

ggplot(sa, aes(x = id, y = value, group = grp, color = type)) + 
    geom_line(size = 6, position = position_dodge(width = 0.1)) + 
    coord_flip() 

dodged plot