I'm making a ggplot bar chart using the following data:
dispensed.date.start dispensed.date.end drug_class
2016-11-28 2016-12-07 opioid
2016-12-08 2016-12-15 benzodiazepene
2016-12-18 2016-12-26 MAT
2016-12-24 2016-12-31 opioid
#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')
Here's the plot:
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)
# Don't show axes, appropriately position legend
timeline_plot<-timeline_plot+theme(
legend.position = "none"
)
# Display years
month_buffer <- 6
year_date_range <- seq(min(PDMP.data.clean$dispensed.date.start) - months(month_buffer), max(PDMP.data.clean$dispensed.date.start) + months(month_buffer), by='year')
year_date_range <- as.Date(
intersect(
ceiling_date(year_date_range, unit="year"),
floor_date(year_date_range, unit="year")
), origin = "1970-01-01"
)
year_format <- format(year_date_range, '%Y')
year_df <- data.frame(year_date_range, year_format)
# Show year text on plot
timeline_plot<-timeline_plot+geom_text(data=year_df, aes(x=year_date_range,y=.9,label=year_format, fontface="bold"),size=2.5, color='black')
print(timeline_plot+ ggtitle(paste("Prescription History for Case # ",PDMP.data.clean$ndc))+
theme(plot.title = element_text(hjust = 0.5),aspect.ratio = 1/3.2,))
There's two things I'd like to fix but I'm not sure how: First, how do I get each month to show up on the a-axis, instead of only every 3rd month the way it currently is shown? Second, is there a way display the month without the year, since I'm already adding the year as text above the month of January?
