Re-posting this question with more detail. I have a ggplot bar plot, each bar indicating a length of time along a timeline. I'm labeling the x-axis tick marks with custom text that shows each month.
#build months dataframe for labeling a-axis
month_buffer <- 6
month_date_range <- seq(min(PDMP.data.clean$dispensed.date.start) - months(month_buffer), max(PDMP.data.clean$dispensed.date.start) + months(month_buffer), by='month')
month_date_range <- as.Date(
intersect(
ceiling_date(month_date_range, unit="month"),
floor_date(month_date_range, unit="month")
), origin = "1970-01-01"
)
month_format <- format(month_date_range, '%b')
month_df <- data.frame(month_date_range, month_format)
Which gives me:
month_date_range month_format
2016-06-01 Jun
2016-07-01 Jul
2016-08-01 Aug
All good up to here. And finally, building the plot:
#draw 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, legend
timeline_plot<-timeline_plot+theme(
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.x =element_blank(),
axis.ticks.x =element_blank(),
legend.position = "none"
)
# add month as text to x-axis
timeline_plot<-timeline_plot+geom_text(data=month_df, aes(x=month_date_range,y=-0.4,label=month_format),size=2.5,vjust=0.5, color='black', angle=90) + coord_cartesian(clip = "off")
Everything works except I can't get the month geom_text to show up below the x-axis! Photo below. I want the month labels to sit below the x-axis.
Is there a simple solution here? Thanks.