1

enter image description here I would like to show tick marks in the center of each bar, but only include labels for every other date. In the past, for continuous data, I've done something like this:

ylabs = seq(0, 4000, 250)
ylabs[-(seq(1, 31, 4))] = ""

scale_y_continuous(breaks = seq(0, 4000, 250), labels = ylabs, limits = c(0,  4000))

This method is annoying, but I cannot figure out how to solve this using ggplot2 functions. My best guess is to adopt my method above as follows:

labels = as.character(unique(Daily.Events$DateOnly))
labels[-seq(from = 1, to = length(labels), by = 2)] = ""

and some how insert the custom labels in the scale_x_datetime() function below:

g = ggplot(Daily.Events, aes(DateOnly, Count))
g = g + geom_bar(stat = "identity", fill = "black")
g = g + scale_x_datetime(name = "Date", 
                         breaks = date_breaks("1 days"),
                         labels = date_format(format = "%b %d"),
                         expand = c(0,0))

g = g + scale_y_continuous(name = "Daily Events")
g = g + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
plot(g)

Can anyone offer a better solution? I appreciate any help and criticism provided!

Dan A.
  • 129
  • 10
  • 1
    What about `date_breaks = "2 day"` instead of `"1 day"`? – aosmith Nov 08 '16 at 15:44
  • Are you sure you have `POSIXt` values and not `Date`s? Anyway, the datetime and date scales have parameters `date_breaks` and `date_labels`. Use these instead if `breaks` and `labels`. – Roland Nov 08 '16 at 15:48
  • i played around with date_breaks and date_labels before posting. they produced the same result. i originally had date_break = "2 day", and it produced the labels i wanted, but did not place a tick mark at the unlabeled bars. – Dan A. Nov 08 '16 at 18:51
  • Can you make your example reproducible by adding a small example dataset? Ideas for how to do this [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). An related example [in this answer](http://stackoverflow.com/a/39259445/2461552) – aosmith Nov 08 '16 at 21:28

0 Answers0