0
votes

I'm making a ggplot bar chart using the following data. Here's the data I'm using for geom_segment:

dispensed.date.start    dispensed.date.end    drug_class         case
2016-11-28              2016-12-07            opioid             12345
2016-12-08              2016-12-15            benzodiazepene     12345
2016-12-18              2016-12-26            MAT                12345
2016-12-24              2016-12-31            opioid             12345

And data for geom_vline:

Case                    Event                  DOD
123456                  death                  2018-01-02

Here's the plot:

#set levels, colors
status_levels <- c("Benzodiazepene", "MAT", "Nerve pain / Anticonvulsant", "Opioid", "Sedative", "Stimulant", "Death")
status_colors <- c("#984ea3", "#a65628", "#4daf4a", "#e41a1c", "#ff7f00", "#377eb8", '#000000')

#Plot bars
timeline_plot<-ggplot(PDMP.data.clean,aes(x=dispensed.date.start, y=.2),show.legend = FALSE) +
geom_segment(aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class,col=drug_class), size=5,show.legend = FALSE) +
  # Plot vertical line for date of death
  geom_vline(data = deathDate, mapping = aes(x = DOD, xintercept = DOD, y = 0),size=.5, show.legend = TRUE)
timeline_plot<-timeline_plot+labs(col="Drugs Prescribed", show.legend = FALSE)
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()+ scale_x_date(date_breaks = "1 month",date_labels = "%b")

# Don't show axes, appropriately position legend
timeline_plot<-timeline_plot+theme(
                                   legend.position = "none"
)

# configure legend
timeline_plot<-timeline_plot+theme(
                                   axis.title.x=element_blank(),
                                   axis.title.y=element_blank(),
                                   legend.position = "bottom"
)
                                  
print(timeline_plot+ ggtitle(paste("Prescription History for Case # ",PDMP.data.clean$case))+
        theme(plot.title = element_text(hjust = 0.5),aspect.ratio = 1/3.2,))

Here's the resulting plot:

enter image description here

I only want the vertical line labeled "death" to show up in the legend, instead, it's showing that vertical line next to every category of drug_class. I'm not sure why this is displaying in the legend since I though I explicitly excluded with this line of code:

timeline_plot<-ggplot(PDMP.data.clean,aes(x=dispensed.date.start, y=.2),show.legend = FALSE) +
geom_segment(aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class,col=drug_class), size=5,show.legend = FALSE)
1

1 Answers

1
votes

On possible solution is to set the linetype in the geom_vline statement and then manual define a legend.
See the modifications to "geom_vline" line and the added "scale_linetype("Legend", labels="Death")" line

timeline_plot<-ggplot(PDMP.data.clean, aes(x=dispensed.date.start, y=.2)) +
  geom_segment( aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class, col=drug_class), size=5, show.legend = FALSE) +
  # Plot vertical line for date of death
  geom_vline(data = deathDate, mapping = aes(x=DOD, xintercept = DOD, linetype = "2"), size=.5) +
  scale_linetype("Legend",  labels="Death")

## If color legend is not plotted the following line isn't needed
# timeline_plot<-timeline_plot+labs(col="Drugs Prescribed")
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()+ scale_x_date(date_breaks = "1 month",date_labels = "%b")

# Don't show axes, appropriately position legend
# timeline_plot<-timeline_plot+theme(legend.position = "none") #not needed

# configure legend
timeline_plot<-timeline_plot+theme(
  axis.title.x=element_blank(),
  axis.title.y=element_blank(),
  legend.position = "bottom")

print(timeline_plot+ 
       ggtitle(paste("Prescription History for Case # ", PDMP.data.clean$case)) +
       theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1/3.2,))

enter image description here