使用第三个属性在 r 中创建时间轴

create a timeline in r with a third attribute

我想为下面给出的数据框创建时间轴。

df <- data.frame(names = LETTERS[1:5], dates = structure(c(20109, 18659, 19079, 19052, 19024), 
                 class = "Date"), place = c(5,7,4,2,6))

我把时间线画成

library(timelineS)
timelineS(df, main = "title", buffer.days = 50, line.width = 2,
          line.color = "darkgray", scale = "month",
          scale.font = 1, labels = paste0(df[[1]]))

如何在每个指针的正下方添加 place 数据列?我不希望它在标签下,而是在 x 轴的日期下。

很难说出您想要这些标签的确切位置(尤其是如果您不希望它们与轴、线和现有标签发生冲突时)。您说您希望它们位于 x 轴下方,但是 E、D 和 C 的标签 运行 相互重叠,结果很糟糕。

timelineS(df, main = "title", buffer.days = 50, line.width = 2,
          line.color = "darkgray", scale = "month",
          scale.font = 1, labels = paste0(df[[1]]))
yvals <- rep(-0.4, nrow(df))
points(x = df$dates, y = yvals, col = 'white', pch = 16, cex = 3)
points(x = df$dates, y = yvals, pch = 21, cex = 3)
text(x = df$dates, y = yvals, label = df$place)

在这里,我已经演示了每个指针的 'stalk' 上的标签,但是在任何情况下都应该很容易修改以下代码以准确地将标签放在您想要的位置。关键是使用函数 text,虽然我首先绘制了一些整齐的小圆圈来放置标签并避免过度绘制:

timelineS(df, main = "title", buffer.days = 50, line.width = 2,
          line.color = "darkgray", scale = "month",
          scale.font = 1, labels = paste0(df[[1]]))

yvals <- 0.4 * c(-1, 1, -1, 1, -1)
points(x = df$dates, y = yvals, col = 'white', pch = 16, cex = 3)
points(x = df$dates, y = yvals, pch = 21, cex = 3)
text(x = df$dates, y = yvals, label = df$place)