Reputation: 19375
Consider this simple example
tibble(date = c(ymd('2021-01-01'),
ymd('2021-04-01')),
value = c(10, 20)) %>%
ggplot(aes(x = date, y = value)) + geom_col()
As you can see, the bar are centered at the date
. Instead, I would like the first bar to span January to March (first quarter) and April to June (second quarter).
How can I do that? Thanks!
Upvotes: 0
Views: 40
Reputation: 887118
Perhaps, we can adjust the scale_x_date
library(dplyr)
library(ggplot2)
library(lubridate)
library(stringr)
start <- seq(min(tbl1$date), max(tbl1$date), by = '3 months')
end <- start %m+% months(2)
start_end <- str_c(format(start, '%b %Y'), format(end, '%b %Y'), sep='--')
tbl1 %>%
ggplot(aes(x = date %m+% days(40), y = value)) +
geom_col() +
scale_x_date(breaks = start, labels = start_end) +
xlab("date")
tbl1 <- tibble(date = c(ymd('2021-01-01'),
ymd('2021-04-01')),
value = c(10, 20))
Upvotes: 1