在 R 中创建时间轴

Creating a Timeline in R

所以我在这里的目的是展示并发健康技术的时间线,它们的持续时间为矩形或平铺几何图形,其宽度表示 x-axis 上指定时间间隔内的持续时间,以及制造商Y-axis.

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

我正在使用 ggplot2timeline 软件包。按照在线找到的示例,我将数据编辑为仅包含 headers 列: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)

然而,当绘制数据时,所有几何图形都叠加在一个组中。我需要帮助间隔,使文本适合 geoms。

好吧,您是按制造商分组的,同一制造商的不同设备具有重叠日期,因此代表设备制造时间的矩形当然重叠,标签也重叠,因为它们居中在(重叠的)矩形中:

你能做的是

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

  • 或禁止绘制标签(使用timeline(... , text.color = "transparent")

    并手动将它们放在不重叠的地方。

就我个人而言,我觉得重叠的矩形不太可读,可能会选择不同的可视化方式,例如每个产品有一个 "row" 的甘特图,类似于下图:

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

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

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

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

您也可以使用 timevis 包来创建交互式绘图,尽管创建组有点麻烦。

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

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