将 R 中的时间线绘制为单个标记条,并在 x 轴标签上显示日期

Plot timeline in R as single labeled bar and with dates at the x-axis labels

我想使用 R 中的时间线包创建一个漂亮的时间线图,但我不确定如何解决我遇到的这些问题。 一是这些条向上堆叠,但我希望它们在同一水平面上。 y 轴应该被排除在外。 x 轴应使用 StartLabel 列中的标签。 我如何到达那里?

这里是代码和数据:

require(data.table)
require(timeline)

setsTimeline <- data.table(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
       StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
       EndDate=  c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))

setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]
setsTimeline[,Group:=c("Timeline")]
setkey(setsTimeline,StartDate)

timeline(setsTimeline,label.col="Set",start.col="StartDate",end.col="EndDate",group.col="Group")

我希望看到这样的内容:http://timelyportfolio.github.io/rCharts_time_series/assets/fig/unnamed-chunk-3.png

但现在看起来像这样:

编辑: 所以也许这是一个失败的原因,因为这个包还没有完成。 我确实设法将图表拆开。看来必须定义一个组变量。 但我仍然无法弄清楚如何更改标签和添加图例。看来这个情节不响应通常的情节功能。 我编辑了上面的代码,这里是当前图表:

很明显,我想做的事情甚至可以只用 ggplot2,所以我想知道为什么有人甚至费心为它创建一个新包。 我又花了几个小时试图找到正确的选项和东西,但这里是:

require(data.table)
require(ggplot2)
require(grid)

setsTimeline <- data.table(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
       StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
   EndDate=  c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))

setsTimeline[,StartLabel:=as.POSIXct(StartDate,tz="UTC",origin="1970-01-01")]

breaks <- c(1380708900,1395100800,1402963200,1410912000,1418860800,1430985600)
labels <- as.POSIXct(breaks,tz="UTC",origin="1970-01-01")

ggplot(setsTimeline, aes(colour=Set)) + 
  geom_segment(aes(x=StartDate, xend=EndDate, y="group", yend="group"), size=10) +
  theme(panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.background = element_blank(),
    aspect.ratio=5e-02, 
    axis.text.x = element_text(colour='black', angle = 45, size = 16, hjust = 1, vjust = 1),
    legend.text = element_text(colour='black', size = 16),
    legend.title = element_text(colour='black', size = 0),
    legend.position = 'top',
    plot.title = element_text(colour='black', size = 18),
    panel.margin = unit(1, "cm")) +
  xlab(NULL) + 
  ylab(NULL) +
  ggtitle("Data timeline with marked sets") +
  coord_cartesian(xlim = c(1380708900,1430985600), ylim = NULL) +
  scale_x_continuous(breaks=breaks,labels=labels)

结果:

如果有人知道如何摆脱 y 轴以及如何让第一个 x 轴标签保留在绘图区域内而不必 move/turn 它,请帮助我.我尝试了 plot.margin 和 panel.margin,但一个什么也没做,另一个一直出错。

现在已经过了我的就寝时间,明天我有一个演示文稿...

有一个新包在做这项工作:vistime。来自 cran 的版本执行以下操作:

library(vistime)
setsTimeline <- data.frame(Set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"),
                           StartDate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200),
                           EndDate=  c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900))
setsTimeline$StartLabel <- as.POSIXct(setsTimeline$StartDate,tz="UTC",origin="1970-01-01")
setsTimeline$EndLabel <- as.POSIXct(setsTimeline$EndDate,tz="UTC",origin="1970-01-01")
vistime(setsTimeline, col.event="Set", col.start="StartLabel", col.end="EndLabel")

reprex package (v2.0.0)

于 2021-06-29 创建