4

i'm trying to make a gantt chart in ggplot2. i'm having troubles changing the colors and spacing of each segment in a geom_segment()

> head(g672)
mobility    start  endtime
1    active  0.00000  1.60157
3    active  1.60157 59.65837
5    active 59.65840 68.93415
7  immobile 68.93420 69.03430
9    active 69.03430 77.87629
11 immobile 77.87620 80.27855

I'm using geom_segment() to generate the chart:

ggplot(g672, aes(colour=mobility)) + 
  geom_segment(aes(x=start, xend=endtime, y=mobility, yend=mobility), size=15) +
  xlab("Duration") +
  theme_classic()

Which generates this chart:

enter image description here

I'd like to be able to do 2 things: 1) change the colors and 2) make the 2 bars appear closer to each other or even get both of them overlapping (these are mutually exclusive categories so if you're not doing one, you're doing the other).

cheers and thanks for any advice

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
R-MASHup
  • 365
  • 2
  • 9

1 Answers1

6

My solution would be to use some character as y and yend values (that will make segments to overlapp). Then you can remove this character from axis with theme() and axis.text.y= and axis.title.y= . Colors you can change with scale_color_manual().

ggplot(g672, aes(colour=mobility)) + 
      geom_segment(aes(x=start, xend=endtime, y="a", yend="a"), size=15) +
      xlab("Duration") +
      theme_classic()+
      scale_color_manual(values=c("black","green"))+
      theme(axis.text.y=element_blank(),
            axis.title.y=element_blank())

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • is it possible at all to make them closer to each other such that the segments are on top of each other? – R-MASHup Oct 19 '14 at 03:56