generated from ecohealthalliance/container-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code to make heatmap of CoV prevalence by bat family and quarter of t…
…he year
- Loading branch information
Showing
4 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#' Make a heatmap of CoV prevalence by bat family and quarter of the year | ||
#' | ||
#' | ||
#' @title plot_quarterly_heatmap | ||
#' @param heat_data summary table with CoV prevalence by bat family and quarter of the year | ||
#' @return | ||
#' @author Cecilia Sanchez | ||
|
||
plot_quarterly_heatmap <- function(heat_data){ | ||
|
||
ggplot(data = heat_data, mapping = aes(x = quarter, y = family, | ||
fill = prop)) + | ||
geom_tile(color = "black") + | ||
scale_fill_viridis_c(name = "Prop. \nCoV +", option = "magma", | ||
direction = -1) + | ||
# have text be labeled white if background fill is dark | ||
geom_text(aes(label = paste0(n, "/", totN), color = prop > 0.15), size = 5) + | ||
scale_color_manual(guide = "none", values = c("black", "white")) + | ||
scale_x_discrete(labels = c("Jan-Mar", "Apr-Jun", "Jul-Sep", "Oct-Dec")) + | ||
ylab("") + xlab("") + | ||
theme_bw() + | ||
theme(panel.grid.major = element_blank(), | ||
panel.grid.minor = element_blank(), | ||
axis.text = element_text(color = "black", size = 14), | ||
legend.text = element_text(size = 14), | ||
legend.title = element_text(size = 16), | ||
legend.box.spacing = unit(0, "pt"), | ||
legend.margin = margin(0,5,0,5), | ||
plot.margin = margin(t = 2, r = 0, b = -10, l = -10)) | ||
|
||
ggsave("outputs/quarterly_prevalence_heatmap.png", width = 6, height = 4, | ||
units = "in", dpi = 600) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#' Prep data to create a heatmap of CoV prevalence by bat family and quarter of the year | ||
#' | ||
#' | ||
#' @title prep_quarterly_heatmap_data | ||
#' @param CoV_prev dataset containing info on capture date, bat family, and CoV test result | ||
#' @return | ||
#' @author Cecilia Sanchez | ||
|
||
prep_quarterly_heatmap_data <- function(CoV_prev){ | ||
|
||
# do some initial data cleaning | ||
cleaned <- CoV_prev %>% | ||
janitor::clean_names() %>% | ||
mutate(date = lubridate::dmy(date), | ||
quarter = as.factor(lubridate::quarter(date))) %>% | ||
rename(cov_result = co_v_final_results) %>% | ||
filter(!is.na(cov_result)) %>% | ||
mutate(cov_result = as.factor(cov_result)) | ||
|
||
# summarize proportion positive for each family and quarter | ||
quarter_family_summary <- cleaned %>% | ||
group_by(family, quarter, cov_result, .drop = F) %>% | ||
dplyr::summarize(n = n()) %>% | ||
mutate(prop = n / sum(n)) %>% | ||
mutate(totN = sum(n)) %>% | ||
filter(cov_result == "Positive") | ||
|
||
# also calculate a summary only by quarter, lumping all families together | ||
quarter_summary <- cleaned %>% | ||
group_by(quarter, cov_result, .drop = F) %>% | ||
dplyr::summarize(n = n()) %>% | ||
mutate(prop = n / sum(n)) %>% | ||
mutate(totN = sum(n)) %>% | ||
filter(cov_result == "Positive") %>% | ||
mutate(family = "Overall") | ||
|
||
# bind everything together to plot | ||
heat_data <- bind_rows(quarter_family_summary, quarter_summary) %>% | ||
mutate_at(vars(family), as.factor) %>% | ||
mutate(family = fct_relevel(family, "Overall")) %>% | ||
drop_na() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters