Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot2 geom_line position dodge

Tags:

r

ggplot2

I can't find an example on how to do this. I'm trying to draw lines using geom_line but dodge the overlapping elements, and it doesn't seem to work.

Here is my data:

> 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

Here is my simple code trying to dodge overlapping values for typ1 and typ2

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

plot I get

This is what I see. How do I dodge the overlapping bars?

like image 667
Sri Avatar asked Dec 10 '22 17:12

Sri


1 Answers

You can only dodge horizontally, but you can get around this issue by flipping your x and y aesthetics and using 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

like image 144
alistaire Avatar answered Dec 24 '22 15:12

alistaire