I have a data.frame of the last 4 weeks and the first 4 weeks of 2022 and 2023. I'm looking to create a bar plot where my x-axis displays the last weeks of 2022 and then the first weeks of 2023. However, ggplot automatically sorts the week numbers ascending. I'm looking to instruct the computer to plot week 49,50,51,52 first and 1,2,3,4 second. Below a set of dummy data to explain the issue I am dealing with.
df.demo <- data.frame(year = c(2022,2022,2022,2022,2023,2023,2023,2023), week = as.integer(49,50,51,52,1,2,3,4), y = c(10,11,12,13,20,21,22,23))
ggplot(df.demo, aes(x=week, y=y)) geom_col()
Produces this plot:
I know I can remove all other weeks by plotting 'week' as a factor but that still does not solve my arrangement issue.
CodePudding user response:
one approach:
library(ggplot2)
library(lubridate)
df.demo <-
df.demo |>
mutate(year = as_date(paste0(year,"-01-01")),
xpos = year as.duration(paste(week, 'weeks'))
)
df.demo |>
ggplot(aes(xpos, y, week))
geom_col(aes(xpos, y))
scale_x_date(breaks = df.demo$xpos, date_labels = '%W / %y')
labs(x = 'week')
Haven't yet found out, why I have to supply df.demo$xpos
instead of only xpos
for the scale_x_date(breaks = ...)
. Any suggestions?