I want to create a timeline for my dataframe given below.
df <- data.frame(names = LETTERS[1:5], dates = structure(c(20109, 18659, 19079, 19052, 19024),
class = "Date"), place = c(5,7,4,2,6))
I plot the timeline as
library(timelineS)
timelineS(df, main = "title", buffer.days = 50, line.width = 2,
line.color = "darkgray", scale = "month",
scale.font = 1, labels = paste0(df[[1]]))
How do I add the place
data column right under each pointer? i don't want this to be under the label, but under the date of the x-axis.
CodePudding user response:
It's hard to tell exactly where you want these labels (especially if you do not want them to clash with the axis, lines, and existing labels). You say you want them beneath the x axis, but then the labels for E, D and C run into each other, giving an ugly result.
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)
Here, I have demonstrated the label on the 'stalk' of each pointer, but in any case it should be easy to modify the following code to get the labels exactly where you want them. The key is to use the function text
, though I have first plotted some neat little circles to put the labels in and avoid overplotting:
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)