我想為下面給出的資料框創建一個時間線。
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 軸的日期下。
uj5u.com熱心網友回復:
很難準確說出您想要這些標簽的位置(尤其是如果您不希望它們與軸、線和現有標簽發生沖突)。您說您希望它們位于 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)
在這里,我已經在每個指標的“莖”上演示了標簽,但無論如何,修改以下代碼以將標簽準確地放在您想要的位置應該很容易。關鍵是使用函式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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/454419.html
