# Function that creates paired plots of maps with percentages in the plot
make_paired_plot<-function(organ,
distance,
outcome,
buffer_list,
year1="2017",
year2="2022",
plottitle,
include_nonHIV=FALSE)
{
# ----Error checking----
# Validate include_nonHIV
if (!is.logical(include_nonHIV) || length(include_nonHIV) != 1 || is.na(include_nonHIV)) {
stop("include_nonHIV must be a single TRUE or FALSE value.")
}
#Access name of object passed to `buffer_list`
buffer_name <- deparse(substitute(buffer_list))
#Make sure name of outcome matches buffer_list
if (!stringr::str_detect(
stringr::str_to_lower(buffer_name),
stringr::str_to_lower(outcome)
)) {
warning(glue::glue(
"Buffer object ('{buffer_name}') may not match outcome '{outcome}'."
))
}
if (!year1 %in% year_list) {
stop(glue::glue("year1 ({year1}) is not in year_list."))
}
if (!year2 %in% year_list) {
stop(glue::glue("year2 ({year2}) is not in year_list."))
}
if (!outcome %in% c("active", "HIV", "HOPE")) {
stop(glue::glue("outcome ({outcome}) is not in c('active', 'HIV', 'HOPE')."))
}
# ----Main function----
#Keep only elements of Results_HIV_df necessary for plot
summary_df_HIV<-Results_HIV_df[[organ]]%>%
filter(str_detect(Characteristic, outcome))%>%
filter(str_detect(Characteristic, distance))
#parameters of plot assigned
parameter_plot<-list()
parameter_plot$y_title<-0.9
parameter_plot$y_footnote<-0.1
if (include_nonHIV)
{
parameter_plot$y_title<-0.97
parameter_plot$y_footnote<-0.01
}
#Keep only elements of Results_nonHIV_df necessary for plot, if even needed in the first place
if (include_nonHIV)
{
summary_df_nonHIV<-Results_nonHIV_df[[organ]]%>%
filter(str_detect(Characteristic, outcome))%>%
filter(str_detect(Characteristic, distance))
}
#Creates text for nonHIV (is NULL if )
title_text_nonHIV<-list()
if (include_nonHIV==TRUE) {
title_text_nonHIV[[year1]]<-
glue("<br>",
"<span style='font-size:30pt; font-weight:normal;'>HIV negative:</span> ",
"<span style='font-size:30pt; font-weight:bold;'>",
"{summary_df_nonHIV$Percentage[summary_df_nonHIV$Year == as.numeric(year1)]}%",
"</span>",
"<br>",
"{comma(summary_df_nonHIV$Numerator[summary_df_nonHIV$Year == as.numeric(year1)])}",
" / ",
"{comma(summary_df_nonHIV$Denominator[summary_df_nonHIV$Year == as.numeric(year1)])}")
title_text_nonHIV[[year2]]<-
glue("<br>",
"<span style='font-size:30pt; font-weight:normal;'>HIV negative:</span> ",
"<span style='font-size:30pt; font-weight:bold;'>",
"{summary_df_nonHIV$Percentage[summary_df_nonHIV$Year == as.numeric(year2)]}%",
"</span>",
"<br>",
"{comma(summary_df_nonHIV$Numerator[summary_df_nonHIV$Year == as.numeric(year2)])}",
" / ",
"{comma(summary_df_nonHIV$Denominator[summary_df_nonHIV$Year == as.numeric(year2)])}")
}
else {
title_text_nonHIV[[year1]]<-""
title_text_nonHIV[[year2]]<-""
}
#Left graph
Plot1<-ggplot() +
geom_sf(data = county_dots_transformed[[year1]], size = 0.3, color = "navy", alpha = 0.5) +
theme_void() +
theme(
axis.title.y = element_text(size = 36, angle = 90, vjust = 0.5),
plot.title = element_textbox(size = 16, hjust = 0.5,
halign = 0.5)
)+
labs(y=year1,
title = glue("<span style='font-size:30pt; font-weight:normal;'>PLWH:</span> ",
"<span style='font-size:30pt; font-weight:bold;'>",
"{summary_df_HIV$Percentage[summary_df_HIV$Year == as.numeric(year1)]}%",
"</span>",
"<br>",
"{comma(summary_df_HIV$Numerator[summary_df_HIV$Year == as.numeric(year1)])}",
" / ",
"{comma(summary_df_HIV$Denominator[summary_df_HIV$Year == as.numeric(year1)])}",
title_text_nonHIV[[year1]]
))+
geom_sf(data=buffer_list[[organ]][[distance]][[year1]], color="red", fill=NA)+
geom_sf(data=states_sf_transformed, fill=NA)
#Right plot
Plot2<-ggplot() +
geom_sf(data = county_dots_transformed[[year2]], size = 0.3, color = "navy", alpha = 0.5) +
theme_void() +
theme(
axis.title.y = element_text(size = 36, angle = 90, vjust = 0.5),
plot.title = element_textbox(size = 16, hjust = 0.5,
halign = 0.5)
)+
labs(y=year2,
title = glue("<span style='font-size:30pt; font-weight:normal;'>PLWH:</span> ",
"<span style='font-size:30pt; font-weight:bold;'>",
"{summary_df_HIV$Percentage[summary_df_HIV$Year == as.numeric(year2)]}%",
"</span>",
"<br>",
"{comma(summary_df_HIV$Numerator[summary_df_HIV$Year == as.numeric(year2)])}",
" / ",
"{comma(summary_df_HIV$Denominator[summary_df_HIV$Year == as.numeric(year2)])}",
title_text_nonHIV[[year2]]
))+
geom_sf(data=buffer_list[[organ]][[distance]][[year2]], color="red", fill=NA)+
geom_sf(data=states_sf_transformed, fill=NA)
#Combine plots
combined<-cowplot::plot_grid(Plot1, Plot2,
ncol = 2, align = "hv")
final_plot <- ggdraw() +
draw_plot(combined) +
draw_label(
plottitle,
x = 0.5, y = parameter_plot$y_title, # top center
hjust = 0.5, vjust = 1, # anchor to the top edge
fontface = 'bold',
size = 25
)+
#
draw_label(
"Each blue dot represents 100 people with HIV\nRed outlines represent the catchment area for each transplant center",
x = 0.02, y = parameter_plot$y_footnote, # left margin position
hjust = 0, vjust = 0, # align text to the left edge
size = 14,
fontface = "italic"
)
return(final_plot)
}