Home > Mobile >  How to show every year for gg_vistime() graph?
How to show every year for gg_vistime() graph?

Time:12-30

I have the following data frame:

structure(list(event = c("X", "Y", "X"), start = c("1994-01-01", 
"2004-01-01", "2013-01-01"), end = c("2003-12-31", "2012-12-31", 
"2018-12-31"), color = c("#EEEEEE", "#CCCCCC", "#999999")), class = "data.frame", row.names = c(NA, 
-3L))

I changed the dates to the date format:

library(vistime)
library(ggplot2)
timeline_data$start <- as.Date(timeline_data$start, format="%Y-%m-%d")
timeline_data$end <- as.Date(timeline_data$end, format="%Y-%m-%d")

I want to see every single one of the years on the x-axis here:

gg_vistime(timeline_data)   scale_x_continuous(breaks=seq(1994,2018,1))

But now the x-axis is blank. Any guidance would be appreciated!

CodePudding user response:

You need scale_x_datetime here. The gg_vistime stores its date-times in a POSIXct scale, which uses seconds (starting from midnight on 1st January 1970), as opposed to years starting at 1 CE, so your breaks were all in the first hour of January 1st 1970. Since your plot doesn't cover this period, there are no visible breaks.

gg_vistime(timeline_data)   
  scale_x_datetime(date_breaks = "year", date_labels = "%Y")

enter image description here

  • Related