0

Using an idea from a previous question I have created a gantt-like chart using ggplot2. Here is the example code:

tasks <- c("Review literature", "Mung data", "Stats analysis", "Write Report")

dfr <- data.frame(
  name        = tasks[c(1,2,3,4,2,3)],
  start.date  = c("24/08/2010", "01/10/2010", "01/11/2010", "14/02/2011","15/12/2010","1/9/2010"),
  end.date    = c("31/10/2010", "14/12/2010", "28/02/2011", "30/04/2011","05/02/2011","1/11/2010"),
  type = c(TRUE, FALSE, TRUE, TRUE,TRUE,FALSE)
)

mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))

ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = type)) + 
  geom_line(size = 6) +
  xlab("") + ylab("") +
  theme_bw()

Now, I need to indicate one (or maybe more, some other day) specific critical date for each task, using a bullet or a star or anything, which maybe inside or outside the bar and also a textual annotation of that date. Can it be achieved using the above procedure. If not, is there another (not ggplot) way of doing this?

Thank you!

Community
  • 1
  • 1
gd047
  • 29,749
  • 18
  • 107
  • 146

1 Answers1

2

Here you go:

require(ggplot2)
tasks <- c("Review literature", "Mung data", "Stats analysis", "Write Report")

dfr <- data.frame(
    name        = tasks[c(1,2,3,4,2,3)],
    start.date  = c("24/08/2010", "01/10/2010", "01/11/2010", "14/02/2011","15/12/2010","1/9/2010"),
    end.date    = c("31/10/2010", "14/12/2010", "28/02/2011", "30/04/2011","05/02/2011","1/11/2010"),
    type = c(TRUE, FALSE, TRUE, TRUE,TRUE,FALSE)
)
dfrLabels <- data.frame(
    name  = tasks[c(1,2,3,4)],
    date  = c("16/10/2010", "07/12/2010", "14/02/2011", "15/04/2011"),
    event = c("Something", "Other", "Whatever", "Deadline")
)

mdfr <- melt(dfr, measure.vars = c("start.date", "end.date"))

ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = type)) + 
    geom_line(size = 6) +
    xlab("") + ylab("") +
    theme_bw() +
    geom_text( data=dfrLabels, aes(x= as.Date(date, "%d/%m/%Y"), label = event), hjust = 0, vjust = 1, colour = "red", size = 5.0 ) + 
    geom_point( data=dfrLabels, aes(x= as.Date(date, "%d/%m/%Y")), size=3.0, colour="black" )
ROLO
  • 4,183
  • 25
  • 41
  • Thanks, but I only need 4 events (as many as the components of the tasks vector). You have also missed the label.date points – gd047 Oct 19 '11 at 15:15
  • 3
    I missed... I gave you an example which you could have tried to improve upon yourself. But anyhow, I now edited the example to include points and having only one label per task. – ROLO Oct 19 '11 at 15:57