在geom_line图上添加点

时间:2011-10-19 13:17:10

标签: r ggplot2

使用来自previous question的想法我使用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)
)

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)这样做的方式?

谢谢!

1 个答案:

答案 0 :(得分: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" )