在R中创建时间轴

问题描述 投票:3回答:3

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

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

我正在使用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。

r ggplot2 timeline
3个回答
4
投票

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

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

enter image description here


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中创建甘特图。


0
投票

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

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

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.