Monthly initiations

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

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

if (params$report_type == "hiv_month") {
  plot_title <- paste0("HIV treatment initiation (", params$reporting_year, ")")
  plot_data <- data$year_df
}


# generate plot
p <- plot_data %>%
  ggplot(aes(x = start_month)) +
  # colorise current months inclusion
  geom_bar(aes(fill = ! as.numeric(format(plot_data$starttre, "%m")) == params$reporting_month),
           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) +
  # sort months so that January is top and December bottom
  scale_x_discrete(drop = FALSE,
                   limits = rev(levels(data$year_df$start_month))) +
  scale_y_continuous(breaks = int_breaks_rounded) +
  scale_fill_manual(values = rev(binary_colors)) +
  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.