bgreen
bgreen

Reputation: 87

generating a timeline graph in R

I was wanting to generate a timeline like this (though it would be preferable that that the bars not text be different colours): http://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/

Is the data below in the correct format to create a timeline? I tried to find code I could amend, but every example I have seen seems to be very specific to the analysis. Any code suggestions would be appreciated.

id <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1)
content <- c("Refdate","CDate", "Rdate", "Disdate", "Disdate", "Exdate", "Odate",
             "Odate", "Odate", "Odate","Odate", "Odate", "Odate", "Odate")
start<- c("05/07/2011", "24/01/2015", "17/09/2012", "24/11/2014", "6/03/2015",
          "13/07/2011", "5/07/2012", "28/09/2012", "27/02/2014", "24/02/2015",
          "11/03/2015", "17/03/2015", "19/03/2015", "30/03/2015")
df <- data.frame(id, content, start, stringsAsFactors=FALSE)
df$start <- as.Date(df$start, "%d/%m/%Y")

Upvotes: 2

Views: 427

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

If you're willing to fiddle with the order of the points (this example does it quick but there are still overlaps which means you'll need to tweak the order a bit more manually or find a good algorithm) then ggrepel may be able to help you:

xdf <- xdf[order(xdf$start, decreasing = TRUE),]

ggplot(xdf, aes(start, 1)) +
  geom_hline(yintercept = 1, linetype="dotted", size=0.25) +
  ggrepel::geom_label_repel(
    aes(label = content), direction = "y", min.segment.length = 0.1
  ) +
  labs(x=NULL, y=NULL) +
  hrbrthemes::theme_ipsum_rc(grid="") +
  theme(axis.text.y=element_blank()) 

enter image description here

Upvotes: 1

alex_555
alex_555

Reputation: 1102

It's not a pretty solution, but is this what you're looking for?

library(chron)
library(ggplot2)
library(dplyr)

# fill up missing dates in your dataset
dates <- as.Date(min(df$start,na.rm=T):max(df$start,na.rm=T),origin="1970-01-01")
x <- data.frame(id=rep(unique(df$id),each=length(dates)),
                content="",
                start=rep(dates, length(unique(df$id))))
df <- bind_rows(df,x)


# create formatted labels
months_list <- c("January", "February", "March", "April", "May", "June", 
                 "July", "August", "September", "October", "November", "December")
df <- df %>%
  mutate(date_format= factor(paste(months(df$start), years(df$start)),
                         levels=apply(expand.grid(months_list, levels(years(df$start))), 1, paste, collapse=" "))) %>%
  arrange(start) %>%
  # create position
  mutate(ypos = 0.01)

ggplot(df, aes(x=date_format, y=ypos))+
         # remove y-axis
         theme(axis.text.y    = element_blank(),
               axis.title.y   = element_blank(),
               axis.line.y  = element_blank(),
               axis.ticks.y   = element_blank())+
  scale_y_continuous(expand=c(0,0), limits=c(0,1.5))+
  # format x-axis
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))+
  # get milestones
  geom_point(aes(fill=content))+
  # write your text
  geom_text(aes(x=date_format, y=0.5, label=content))

As didn't know which layout you want to achive, I created a basic plot. The layout is now up to you using ggplot2. You can now edit the data and the plot further, i.e. color the points, exclude only points that are connected to milestones, insert lines to these points, create different heights for the text etc. I hope this helps to go from here.

Upvotes: 0

Related Questions