# \donttest{
# artificial data
d <- seq(floor_t(today(), "y"), ceiling_t(today(), "y", ceiling = "last"))
df <- data.frame(d = d, D = as.Date(d), y = cumsum(rnorm(length(d))),
q = paste0("Q", quarter(d)))
# load ggplot2
have_ggplot2 <- require("ggplot2", quietly = FALSE)
# default scale
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw()
}
# compare with the default scale for Date
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = D, y = y)) + theme_bw()
}
# change format
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(format = "%b '%y")
}
# set breaks every 4 months
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(breaks = "4m")
}
# set limits
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y), na.rm = TRUE) + theme_bw() +
scale_x_tind(limits = c(today() %-m% 4, today()))
}
# facets with custom formatting and reduced number of breaks
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(n.breaks = 4, format = "%b '%y") +
facet_wrap(~q, scales = "free_x")
}
# }
Run the code above in your browser using DataLab