在R中使用ggplot2创建多项目时间线 [英] Creating a Multi-Project Timeline Using ggplot2 in R

查看:93
本文介绍了在R中使用ggplot2创建多项目时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含项目各个阶段的多项目时间表,以便在一个易于可视化的地方组织我们当前正在进行的所有项目。

I'm attempting to create a multi-project timeline that features various stages of the project in order to organize all of our current projects going forward in one easy to visualize place.

我最初找到了 timeline()包,并且能够半创建我想要的东西。但是,该软件包似乎不是非常可定制的,因此我希望在 ggplot2()中完成我的时间表。

I initially found the timeline() package and was able to semi-create what I was hoping for. However, it appears that package is not very customizable, so I am hoping to complete my timeline in ggplot2() instead.

这是我的假数据:

Phase Project StartDate EndDate  
SD Test1 2015-08-01 2015-08-31  
DD Test1 2015-08-31 2015-09-30  
CD Test1 2015-09-30 2015-11-14  
PC Test1 2015-11-14 2015-12-14  
CA Test1 2015-12-14 2016-08-10  
SD Test2 2015-09-01 2015-10-01  
DD Test2 2015-10-01 2015-10-31  
CD Test2 2015-10-31 2015-12-15  
PC Test2 2015-12-15 2016-01-14  
CA Test2 2016-01-14 2017-01-08  
SD Test3 2016-01-01 2016-01-13  
DD Test3 2016-01-13 2016-01-25  
CD Test3 2016-01-25 2016-02-12  
PC Test3 2016-02-12 2016-03-13  
CA Test3 2016-03-13 2017-01-07  
SD Test4 2015-06-01 2015-06-01  
DD Test4 2015-06-01 2015-06-01  
CD Test4 2015-06-01 2015-06-01  
PC Test4 2015-06-01 2015-07-01  
CA Test4 2015-07-01 2015-07-01  
SD Test5 2015-05-01 2015-05-25  
DD Test5 2015-05-25 2015-06-18  
CD Test5 2015-06-18 2015-07-24  
PC Test5 2015-07-24 2015-08-23  
CA Test5 2015-08-23 2015-08-23  

如您所见,每个项目都有5个阶段(SD,DD,CD,PC,CA)列出,尽管某些项目(Test4)将没有几个阶段,因为它们当前列出了相同的日期期限。

As you can see, each project has 5 stages (SD, DD, CD, PC, CA) listed even though some projects (Test4) will not have a couple of stages as they currently have the same date periods listed.

当我使用 timeline()运行代码时,这就是我得到的:

When I ran my the code with timeline(), this is what I got:

    data$StartDate<- as.Date(data$StartDate)
    data$EndDate<- as.Date(data$EndDate)
    timeline(data, text.size=4)

因此,正如您所看到的,在某些情况下阶段会重叠,我无法

So as you can see, the phases get overlapped in some situations and I can't adjust fonts or center the text in their respective boxes.

由于这个原因,我试图移至ggplot,但无法弄清楚如何使其相似到此时间轴。

Because of this, I am attempting to move to a ggplot, but can't figure out how to make it similar to this timeline.

到目前为止,这是我的新代码:

So far this is my new code:

ggplot(data,aes(x=data$StartDate, y=data$Project)) +
  geom_line()+
  geom_point()+
  geom_text(aes(label=data$Phase), hjust=0,vjust=0)

目前看来,xlim太小,无法显示我几个项目的整个CA阶段。另外,我希望能够自定义文本,按最接近的开始日期将其重新排序为最新的文本,更改xlab和ylab以及其他我认为不可能在 timleline()中进行的图形更改软件包。

As it currently stands, the xlim is too small to show the entire CA phase of a couple of my projects. Also, I'm hoping to be able to customize the text, reorder it by nearest start date to latest, change xlab and ylab and other graphical changes that I don't believe are possible in the timleline() package.

感谢您的所有帮助,如果需要任何说明,请告诉我!

Thanks for any and all help and please let me know if you need any clarification!

推荐答案

我选择使用线段而不是色带,因为它的直线和线段更易于使用离散的y轴。

I chose to use segment and not ribbon because it's straight lines and segments are much easier to work with with a discrete y-axis.

#make ordered factor so that sequence in legend matches sequence in plot
data$Phase_ordered <- factor(data$Phase,levels=c("SD","DD","CD","PC","CA"))

p1 <- ggplot(data,aes(x=StartDate, y=Project, color=Phase_ordered)) +
  geom_segment(aes(x=StartDate,xend=EndDate,yend=Project),size=15) +
 scale_colour_discrete(guide=guide_legend(override.aes=list(size=7))) #or legend will be too big
p1

这篇关于在R中使用ggplot2创建多项目时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆