2016-04-20 178 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

相關問題