0

I need to build a barplot showing the sold qty of three different services on a weekly basis. I need to add as x-axis label either the calendar week and the month. See the image for an example:

Here is my code:

sample <- data.frame(service_type = c("A","B","C","A","B","C","A","B","C","A","B","C","A","B","C","A","B","C","A","B","C"),
                     qty = c(38,185,87,29,12,133,2,14,31,2,9,59,60,43,137,135,31,159,15,32,1),
                     year_week = c("2022 - CW02","2022 - CW02","2022 - CW02","2022 - CW03","2022 - CW03","2022 - CW03","2022 - CW04","2022 - CW04","2022 - CW04","2022 - CW05","2022 - CW05","2022 - CW05","2022 - CW06","2022 - CW06","2022 - CW06","2022 - CW07","2022 - CW07","2022 - CW07","2022 - CW08","2022 - CW08","2022 - CW08"),
                     xlabel2 = c("Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22")
)

## To use annotate
sample %>%
  #arrange(desc(dat_cal_week_id)) %>%
  ggplot() +
  aes(x = year_week, fill = service_type, weight = qty) +
  geom_bar(position = "dodge") +
  scale_fill_hue(direction = 1) +
  theme_minimal() +
  #theme(legend.position = "left", axis.text.x = element_text(angle=90, hjust=1)) +
  theme(legend.position = "left", axis.text.x = element_blank()) +
  coord_cartesian(clip = "off") +
  annotate(geom = "text",
           x = 1:(nrow(sample)/3),
           y = min(sample$qty),
           label = unique(sample$xlabel2),
           vjust = 1,
           angle = 90)

## Using facet_wrap
sample %>%
  #arrange(desc(dat_cal_week_id)) %>%
  ggplot() +
  aes(x = year_week, fill = service_type, weight = qty) +
  geom_bar(position = "dodge") +
  scale_fill_hue(direction = 1) +
  theme_minimal() +
  theme(legend.position = "left", axis.text.x = element_text(angle=90, hjust=1)) +
  facet_wrap(~xlabel2, strip.position = "bottom") +
  theme(strip.placement = "outside")

The first try is with annotate but I receive the error 'Error in annotate(): ! Unequal parameter lengths: x (7), label (2)'

The second try is with facet_wrap and the plot is repeating the first axis labels twice and it's not what I'm looking for See image

I'm not sure that any of these approaches are correct. I really appreciate anyone who can help me with this. Thanks ​ ​ ​ ​

romina
  • 11
  • 2

2 Answers2

1

One option would be to use facetting but with a free scale, i.e. scales="free_x". Additionally at least for your example data I would switch to facet_grid as it allows to use space="free_x" so that we get bars of equal width per facet:

library(ggplot2)

sample$xlabel2 <- factor(sample$xlabel2, c("Jan-22", "Feb-22"))

ggplot(sample) +
  aes(x = year_week, fill = service_type, weight = qty) +
  geom_bar(position = "dodge") +
  scale_fill_hue(direction = 1) +
  theme_minimal() +
  theme(legend.position = "left", axis.text.x = element_text(angle = 90, hjust = 1)) +
  facet_grid(~xlabel2, switch = "x", scales = "free_x", space = "free_x") +
  theme(strip.placement = "outside")

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
0

If you convert year_week to a Date object, you can use sec.axis in scale_x_date():

library(dplyr)
library(ggplot2)

sample <- sample %>%
  mutate(date = as.Date(paste0(year_week, 1), "%Y - CW%U%u")) 

sample %>%
  ggplot() +
  aes(x = date, fill = service_type, weight = qty) +
  geom_bar(position = "dodge") +
  scale_fill_hue(direction = 1) +
  scale_x_date(
    date_labels = "%Y - CW%U",
    date_breaks = "1 week",
    sec.axis = dup_axis(
      breaks = as.Date(paste0("15-", unique(sample$xlabel2)), "%d-%b-%y"),
      labels = \(x) format(x, "%b-%y")
    )
  ) +
  theme_minimal() +
  theme(legend.position = "left", axis.text.x.bottom = element_text(angle=90, hjust=1))

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • Hi zephryl, this solution works for me. I need to add a line chart over the bar that represents the total qty for each calendar week. I have tried several workaround with geom_line but nothing seems working. Any suggestion? – romina Apr 04 '23 at 14:27
  • I would post the problem as a new question, including the code you've tried. – zephryl Apr 04 '23 at 15:36
  • I have posted below an issue that I have with a bigger dataset. Any comments would be appreciated. – romina May 15 '23 at 16:04