0

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.

enter image description here

Is there a simple solution here? Thanks.

DiamondJoe12
  • 1,611
  • 4
  • 24
  • 55
  • 2
    You'll need to set the y value for the labels to be below where you want the x-axis to be drawn and then set the y-axis lower limit to be where you want the x-axis. In addition, to get the text outside the axis to be visible you need to turn off clipping with `coord_cartesian(clip = "off")`. But is there some reason you can't just set the x-axis labels to the values you desire and avoid `geom_text`? If you provide some sample data, we may be able to help. – eipi10 Feb 26 '20 at 19:26
  • 1
    Agreed, it definitely seems like a better idea to use the inbuilt x-axis labels rather than generating your own! – cardinal40 Feb 26 '20 at 20:07
  • eipi10 - "You'll need to set the y value for the labels to be below where you want the x-axis to be drawn" - is this done through ylim or expand_limits? – DiamondJoe12 Feb 26 '20 at 21:02
  • `ylim` or `scale_y_continuous` (`ylim` is a shortcut). `expand_limits` is permissive. It won't stop the lower limit from going lower if you include data that's below the limit. In `scale_y_continuous`, you'll also probably want to set `expand=c(0,0)` or maybe something like `expand_limits=expand_scale(mult=c(0,0.03))` to remove padding on the lower limit. – eipi10 Feb 26 '20 at 21:12
  • But I still recommend using the built-in x-axis labeling and setting the labels to be what you want them to be. – eipi10 Feb 26 '20 at 21:14
  • why can't I do something like: timeline_plot – DiamondJoe12 Feb 26 '20 at 21:34
  • You can't do ` timeline_plot – dario Feb 26 '20 at 21:50
  • The reason we ask for a data sample + code (a [reproducible example](https://stackoverflow.com/a/5963610/496488)) is to make it easier for us to understand your problem and provide a tailored solution and to avoid the unproductive back-and-forth we're having now. You're much more likely to get an optimal solution (and to get more people willing to spend time trying to solve your problem) if you make it easy by providing a reproducible example. – eipi10 Feb 26 '20 at 21:57
  • thank you all. I've reposted here: https://stackoverflow.com/questions/60423567/including-all-months-in-a-year-on-x-axis-using-ggplot?noredirect=1#comment106891383_60423567 – DiamondJoe12 Feb 27 '20 at 14:18

0 Answers0