Quarterly initiations

# define report specific variables
if (params$report_type == "hcv_month") {
  plot_title <- paste0("Quarterly HCV treatment initiation")
  plot_data <- report_data
}

if (params$report_type == "tb_month") {
  plot_title <- paste0("Quarterly TB treatment initiation")
  plot_data <- report_data %>%
    filter(regimen %in% c("Only 1st line drugs",
                          "Regimen including 2nd line drugs")) 
}

if (params$report_type == "hiv_month") {
  plot_title <- paste0("Quarterly HIV treatment initiation")
  plot_data <- report_data
}


# calculate all quarters to report
yq_df <- expand.grid(year = (params$reporting_year - 4):params$reporting_year,
            quarter = 1:4,
            stringsAsFactors = FALSE)

yq_df$year_quarter <- yq_df$year + yq_df$quarter / 10

yq_df <- yq_df %>% 
  dplyr::arrange(year_quarter) %>% 
  dplyr::filter(year_quarter <= lubridate::quarter(end_date_reporting, with_year = TRUE)) %>% 
  # restrict output to 4 years
  dplyr::top_n(17, wt = year_quarter)



# generate plot
p <- plot_data %>%
  dplyr::mutate(start_quarter = factor(start_quarter,
                                       levels = yq_df$year_quarter)) %>%
  dplyr::filter(! is.na(start_quarter)) %>%
  ggplot(aes(x = start_quarter)) +
  # colorise current months inclusion
  geom_bar(aes(fill = start_quarter == lubridate::quarter(end_date_reporting, with_year = TRUE)),
           alpha = 0.8) +
  # add monthly count as text to bars
  stat_count(aes(y = min(..count.., na.rm = TRUE) * 0.7, label=..count..),
             geom="text",
             color = "white", size = 3) +
  scale_x_discrete(drop = FALSE) + 
  scale_fill_manual(values = binary_colors) +
  scale_y_continuous(breaks = int_breaks_rounded) +
  coord_flip() +
  labs(title = plot_title,
       x = "",
       y = "Number") +
  theme(legend.position = "none")

# # facet plots by regimen type for TB report
if (params$report_type == "tb_month") {
  p <- p + facet_grid(cols = vars(regimen))
}

p


JayAchar/hisreportr documentation built on March 18, 2020, 5:57 a.m.