在R中创建时间轴

时间:2017-05-30 14:51:34

标签: r ggplot2 timeline

所以我的目的是显示并发健康技术的时间表,其持续时间为矩形或瓦片geom,其宽度表示x轴上指定时间间隔内的持续时间,以及制造商沿Y轴的持续时间

我有16个案例,来自4个不同的制造商,从2001-01-01到2016-03-31

我正在使用ggplot2timeline个软件包。根据在线发现的示例,我将我的数据编辑为只包含列标题:DeviceManufacturerStartDateEndDate,并确保没有NULL案例。因此,我为许多迄今仍获许可的技术添加了人工结束日期。

再次尝试使用示例数据,我们有:

    device.data <- data.frame(
    DeviceName = c("Cypher Sirolimus DES", "Taxus Express 2", "Cypher Select Sirolimus DES",
              "Cypher Select Plus Sirolimus DES", "Taxus Liberte", "Endeavor ABT578",
              "Endeavor Sprint Zotarolimus DES", "Xience V", "Taxus Element Monrail ION",
              "Xience Nano", "Promus Element Plus", "Xience Prime",
             "Endeavor Resolute DES","Endeavor Resolute Integrity DES", "Promus Premier", "Xience Xpedition LL and SV"),
    DeviceManufacturer = c("Cordis Cashel","Boston Scientific","Cordis Cashel",
                     "Cordis Cashel","Boston Scientific","Medtronic Inc",
                     "Medtronic Inc", "Abbott Vascular", "Boston Scientific",
                     "Abbott Vascular","Boston Scientific", "Abbott Vascular",
                     "Medtronic Inc", "Medtronic Inc","Boston Scientific", "Abbott Vascular"),
    start_date = as.Date(c("2002-11-15", "2003-09-09", "2005-10-21", 
                     "2006-10-25","2008-02-05", "2008-02-27",
                     "2009-06-10", "2009-08-21", "2011-08-19",
                     "2011-10-24", "2012-01-30", "2012-04-10",
                     "2012-04-14", "2013-03-07", "2013-09-30", "2014-02-19")),
    end_date = as.Date(c("2007-07-18", "2010-11-10", "2007-07-18",
                   "2013-04-05", "2013-11-01", "2016-03-31",
                   "2016-03-31", "2016-03-31", "2011-09-16",
                   "2016-03-31", "2016-03-31", "2016-03-31",
                   "2016-03-31", "2016-03-31", "2016-03-31", "2016-03-31")),
    stringsAsFactors = FALSE
    )
    #data visualization
    timeline(device.data)

然而,在绘制数据时,所有的geom都叠加在一个组中。我需要帮助间隔,使文本符合geoms。

3 个答案:

答案 0 :(得分:4)

使用包vistime和您提供的device.data

library(vistime)
vistime(device.data, events = "DeviceName", groups = "DeviceManufacturer", 
                     start = "start_date", end = "end_date")

enter image description here

答案 1 :(得分:1)

嗯,你是按制造商进行分组,同一制造商的不同设备有重叠日期,所以当然表示设备制造时间的矩形重叠,标签也是如此,因为它们居中于(重叠)矩形:

enter image description here

你能做的是

  • 减小标签的大小,直到它们不再重叠(使用timeline(... , text.size = 1)

    enter image description here

  • 或禁止标记的绘图(使用timeline(... , text.color = "transparent")

    enter image description here

    并手动将它们放置在不重叠的位置。

就个人而言,我没有发现重叠的矩形非常易读,并且可能会选择不同的可视化,例如每个产品有一个“行”的甘特图,类似于下图:

enter image description here

这里的颜色代表制造商,显示哪些设备是由同一家公司制造的,并且由于设备不重叠,因此产品的使用寿命清晰可见。

Here are some answers解释了如何在R中创建甘特图。

答案 2 :(得分:0)

你也可以使用timevis包创建交互式图,尽管创建组有点痛苦。

可以像这样创建未堆叠的图(使用您的数据):

library(timevis)
colnames(device.data) <- c("content", "group", "start", "end")
timevis(device.data)

enter image description here