在ggplot中更改scale_x_date的中断?

时间:2016-04-01 13:53:47

标签: r ggplot2

我想将情节中的休息时间从8 10 12 14 16 18更改为9 11 13 15 17 19

enter image description here

这是我用来传播情节的代码:

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 ~ .) 

1 个答案:

答案 0 :(得分:1)

我认为您必须手动设置breaks序列。

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")

使用lubridate ...

可能会有一些更优雅的方法