3

Tough to explain using words, so please see the image below. I know I can facet by year, but that does not accomplish what I want.



Is this possible?
clean year-month

library(tidyverse)
library(lubridate)

set.seed(88)
start <- ymd("2009/11/01")
data <- data_frame(date = start + months(0:11)
              , donuts = rnorm(12, mean = 30, sd = 5) %>% round(., 0)
              , year = year(date)
              , month = month(date, label =T, abbr =T)
              )

ggplot(data = data, aes(x = date, y = donuts)) + geom_line()
zx8754
  • 52,746
  • 12
  • 114
  • 209
Blake S.
  • 401
  • 1
  • 4
  • 10

2 Answers2

1

You can get pretty close with facetting,

dummy <- mutate(data[3,], date = date - 1L, year = year(date)) 
data <- rbind(data, dummy)

ggplot(data = data, aes(x = date, y = donuts, group=1)) + geom_line() +
  facet_grid(.~year, scale="free", space="free", switch = "x") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b", expand = c(0,0)) +
  theme_minimal() + theme(panel.spacing.x = unit(0,"line"), 
                          strip.placement = "outside")

M--
  • 25,431
  • 8
  • 61
  • 93
baptiste
  • 75,767
  • 19
  • 198
  • 294
0

If someone doesn't know a slicker way to do this, you could do this with a bit of a work around. Make the months an ordered factor, starting with Nov 2009 through Oct 2010. Then you could increase the space beneath the x-axis lables and add 2009 and 2010 as text underneath them. I think you can then set the x-axis tick length manually with axis.ticks.length within your theme, making the ones after Dec 2009 and Oct 2010 longer...

MPhD
  • 456
  • 2
  • 9