Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

create a timeline in r with a third attribute

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]]))

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)

enter image description here
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)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading