在 geom_line 图上添加点

发布于 2024-12-10 21:40:38 字数 895 浏览 0 评论 0原文

使用上一个问题中的想法,我创建了一个类似甘特图的图表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)方法可以做到这一点?

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-12-17 21:40:38

干得好:

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

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" )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文