2

everybody

with the following code in R, I have display a simple gantt chart :

dat <- read.csv2(text="start;duration
 1;4
 7;3
 15;2
 ")
 plot(NA, xlim=c(0,20), ylim=c(0,9), ylab="",xlab="X", xaxt="n", yaxt="n")
 with(dat, segments(x0=start, x1=start+duration, y0=2,y1=2))
 with(dat, text( start+duration/2, 2.5, labels=duration))
 axis(1, at=seq(0,20,by=2), labels=seq(0,20,by=2))

enter image description here

Now How could I modify this code to be able from these data in a csv file :

A; 2; 7;
B; 5; 10;
C; 5; 12;
D; 16; 22;
E; 18; 20;

to plot such a gantt chart

enter image description here

Thanks a lot, for any reply !

Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82
  • 2
    Possible duplicate: [LINK](http://stackoverflow.com/questions/9862519/gantt-style-time-line-plot-in-base-r) – Tyler Rinker May 07 '12 at 21:46
  • @TylerRinker, Thanks a lot for your post. I've try It! and It work fine, but It still something missing : how to handle the ends of lines that are shaped balls and line which it label ? thanks for your time! – Fopa Léon Constantin May 07 '12 at 22:09

2 Answers2

8

Extending the answer suggested by @Tyler Rinker:

library(ggplot2)

df <- read.table(text="Task, Start, End
A,2,7
B,5,10
C,5,12
D,16,22
E,18,20", 
                 header=TRUE,
                 sep = ',')

p <- ggplot(df, aes(colour=Task))
p <- p + theme_bw()
p <- p + geom_segment(aes(x=Start, 
                          xend=End, 
                          y=Task, 
                          yend=Task), 
                      size=2)
p <- p + geom_point(aes(x=Start,
                        y=Task),
                    size=5)
p <- p + geom_point(aes(x=End,
                        y=Task),
                    size=5)
p <- p + geom_text(aes(x=Start-0.5,
                       y=Task,
                       label=Task),
                   fontface="bold")
p <- p + opts(legend.position="None",
              panel.grid.major = theme_blank(),
              axis.text.y = theme_blank())
p <- p + xlab("Duration")
p

Produces:

ggplot2 Gantt Chart

EDIT to produce centred labels

library(ggplot2)

df <- read.table(text="Task, Start, End
A,2,7
B,5,10
C,5,12
D,16,22
E,18,20", 
                 header=TRUE,
                 sep = ',')

df$TaskLabel <- df$Task
df$Task <- as.numeric(df$Task)

p <- ggplot(df, aes(colour=TaskLabel))
p <- p + theme_bw()
p <- p + geom_segment(aes(x=Start, 
                          xend=End, 
                          y=Task, 
                          yend=Task), 
                      size=2)
p <- p + geom_point(aes(x=Start,
                        y=Task),
                    size=5)
p <- p + geom_point(aes(x=End,
                        y=Task),
                    size=5)
p <- p + geom_text(aes(x=(Start+End)/2,
                       y=Task+0.25,
                       label=TaskLabel),
                   fontface="bold")
p <- p + opts(legend.position="None",
              panel.grid.major = theme_blank(),
              axis.text.y = theme_blank())
p <- p + xlab("Duration")
p

Which in turn produces:

ggplot2 with centred labels

daedalus
  • 10,873
  • 5
  • 50
  • 71
  • Thanks for you code, quite simple and fine. How can I do to have label centered upon associated line. Thanks for your time. – Fopa Léon Constantin May 08 '12 at 11:04
  • @FopaLéonConstantin -- That has needed changes at a couple of places of the code, and I thought it better to give you the whole recipe again. Enjoy! – daedalus May 08 '12 at 12:05
0

using package vistime:

install.packages('vistime')
library(vistime)
df <- read.table(text="Task, Start, End
A,2,7
B,5,10
C,5,12
D,16,22
E,18,20", header=TRUE, sep = ',')
df$Start <- as.Date("2017-01-01") + df$Start
df$End <- as.Date("2017-01-01") + df$End
vistime(df, start="Start", end="End", events="Task")

enter image description here

https://github.com/shosaco/vistime

shosaco
  • 5,915
  • 1
  • 30
  • 48