2011-10-19 80 views
0

使用來自previous question的想法我使用ggplot2創建了一個類似甘特圖的圖表。下面是示例代碼:在geom_line圖上添加點

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() 

現在,我需要指明一個(或者更多,有的一天)爲每個任務的特定關鍵日期,使用子彈或星形或任何東西,這也許內部或外部酒吧以及該日期的文字註釋。是否可以使用上述過程來實現。如果沒有,是否有另一種(不是ggplot)的方式呢?

謝謝!

回答

2

在這裏你去:

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") 
+0

謝謝,但我只需要4個事件(多達任務向量的分量)。你也錯過了我想念的label.date點 –

+3

......我給你一個你可能試圖改善自己的例子。但無論如何,我現在編輯示例以包含點並且每個任務只有一個標籤。 – ROLO