Ignacio
Ignacio

Reputation: 7938

change breaks for scale_x_date in ggplot?

I would like to change the break in the plot from 8 10 12 14 16 18 to 9 11 13 15 17 19

enter image description here

This is the code I used to genetate the plot:

library(ggplot2)
df.plot <-structure(list(color = structure(c(2L, 2L, 3L, 1L, 3L, 4L, 3L, 
                                             1L, 4L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 
                                             3L, 3L, 3L, 3L), .Label = c("54", "55", "61", "69"), class = "factor"), 
                         date = structure(c(16687, 16687, 16687, 16687, 16687, 16687, 
                                            16688, 16688, 16688, 16689, 16689, 16690, 16693, 16693, 16693, 
                                            16694, 16694, 16695, 16695, 16695, 16695, 16696, 16696, 16696, 
                                            16696, 16696, 16696), class = "Date"), facet = c("A", 
                                                                                             "A", "A", "A", "A", "B", 
                                                                                             "B", "A", "B", "B", "B", "B", 
                                                                                             "B", "B", "B", "B", "A", "B", 
                                                                                             "A", "B", "A", "C", "B", "C", 
                                                                                             "C", "B", "C")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                  -27L), .Names = c("color", "date", "facet"))

ggplot(df.plot, aes(x=date, fill=color)) + 
  geom_dotplot(binwidth=1, stackgroups=TRUE, binpositions="all") +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  scale_x_date(date_labels = "%b %d", date_breaks = "2 day") +
  theme(
    axis.text.x = element_text(angle = 90, hjust = 1)
  )+
  facet_grid(facet ~ .) 

Upvotes: 2

Views: 849

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226192

I think you have to set up the breaks sequence manually.

brk_vec <- seq.Date(as.Date("2015-09-09"),
                    as.Date("2015-09-19"),by="2 days")
ggplot(...) + scale_x_date(breaks=brk_vec,date_label="%b %d")

There may be some more elegant way to do this with lubridate ...

Upvotes: 1

Related Questions