Adding bubbles to a gantt chart to indicate whether an end date has happened or not ggplot2

Viewed 127

I am creating a gantt chart to monitor adverse events for a clinical trial. I have created a gantt chart that plots how many days from their enrollment date in the study the adverse event started and ended for each adverse event. However, some adverse events have not ended yet, so I was hoping to add some bubbled to indicate the start and end, if the end has actually happened. I have attached a photo of an example that I found on the web for what I am trying to achieve. You can see for ECG-Twave inversion and cough, there are no bubbles indicating the adverse event is still ongoing.

Sample figure

I have attached my dataset and code below. Right now, in order to get the length of the bar, if the end date was NA (indicating the event is still ongoing), I have replaced that with todays date. I am curious if theres a way to add bubbles to the start and end of the bars only if the end date has occurred which is determined by the outcome variable. In other words, I would like a bubble at the start of each bar and a bubble ONLY at the ends of events 1, 2, and 4. Since events 3 and 5 have an outcome of "not resolved", they will not get bubbles at the end.

data <- data.frame(aeterm = c("event1", "event2", "event3", "event4", "event5"),
                   grade = c("mild", "severe", "mild", "severe", "severe"),
                   outcome = c("resolved", "resolved", "not resolved", "resolved", "not resolved"),
                   aestartdate = c(1, 5, 4, 5, 10),
                   aeenddate = c(5, 10, 6, 20, 30))

data_long <- data %>%
  select(aeterm, grade, outcome, aestartdate, aeenddate) %>%
  gather(date.type, task.date, -c(aeterm, grade, outcome)) %>%
  arrange(date.type, task.date)

theme_gantt <- function(base_size=11, base_family="Source Sans Pro Light") {
  ret <- theme_bw(base_size, base_family) %+replace%
    theme(panel.background = element_rect(fill="#ffffff", colour=NA),
          axis.title.x=element_text(vjust=-0.2), axis.title.y=element_text(vjust=1.5),
          title=element_text(vjust=1.2, family="Source Sans Pro Semibold"),
          panel.border = element_blank(), axis.line=element_blank(),
          panel.grid.minor=element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.major.x = element_line(size=0.5, colour="grey80"),
          axis.ticks=element_blank(),
          legend.position="bottom",
          axis.title=element_text(size=rel(0.8), family="Source Sans Pro Semibold"),
          strip.text=element_text(size=rel(1), family="Source Sans Pro Semibold"),
          strip.background=element_rect(fill="#ffffff", colour=NA),
          panel.spacing.y=unit(1.5, "lines"),
          legend.key = element_blank())
  
  ret
}

x.breaks <- seq(length(data_long$aeterm) + 0.5 - 3, 0, by=-3)
timeline <- ggplot(data_long, aes(x=aeterm, y=task.date, colour=grade)) +
  geom_line(size=6) +
  geom_vline(xintercept=x.breaks2, colour="grey80", linetype="dotted") +
  guides(colour=guide_legend(title=NULL)) +
  labs(x=NULL, y=NULL) + coord_flip() +
  # scale_y_date(date_breaks="1 months", labels=date_format("%b ‘%y")) +
  theme_gantt() + theme(axis.text.x=element_text(angle=45, hjust=1))
timeline

Current Graph I Created

2 Answers

Try this approach at the end. You can use your original data with geom_point() to add the bubbles style based on conditions using outcome variable. Here the code:

library(ggplot2)
#Code
timeline <- ggplot(data_long, aes(x=aeterm, y=task.date, colour=grade)) +
  geom_line(size=6) +
  geom_point(data=data,aes(y=aestartdate,x=aeterm,fill=grade),
             color='black',size=5,shape=21,show.legend = F)+
  geom_point(data=data,aes(y=ifelse(outcome=='resolved',aeenddate,NA),
                           x=ifelse(outcome=='resolved',aeterm,NA),fill=grade),
             color='black',size=5,shape=21,show.legend = F)+
  geom_vline(xintercept=x.breaks, colour="grey80", linetype="dotted") +
  guides(colour=guide_legend(title=NULL)) +
  labs(x=NULL, y=NULL) + coord_flip() +
  theme_gantt() + theme(axis.text.x=element_text(angle=45, hjust=1))

Output:

enter image description here

An alternative would be:

timeline + 
  geom_point(data = subset(data_long, date.type == "aestartdate" | 
                             outcome == "resolved"),
             shape = 21, size = 8, aes(fill = grade), color = "black") +
  guides(fill = guide_none())

enter image description here

Related