Created
January 8, 2019 14:04
-
-
Save gabrielburcea/465aaa7214daf81c8b4f007c920ade57 to your computer and use it in GitHub Desktop.
length of stay with stacked bars - with reduced bins
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
los_distrib_with_better_breaks <- function(data, plot_chart){ | |
# renaming the variables I am interested in | |
df_rename <- df %>% | |
select(PAT_CODE, START_DATETIME, END_DATETIME, episode.order, Admission.Type, disch.ward.type, WARD_CODE) %>% | |
dplyr::rename(IDcol = PAT_CODE, Admissions = START_DATETIME, | |
Discharges = END_DATETIME, Episode_number = episode.order, Ward_code = WARD_CODE, | |
Admission_type = Admission.Type, Discharge_ward_type = disch.ward.type) | |
#df_subset <- df_subset %>% | |
#filter(EpisodeStart > StartDate & EpisodeEnd < EndDate) | |
#here try not to define the StartDate at the beggining of the function and try to set up a date that works | |
#Warning messages: | |
#1: In Ops.factor(EpisodeStart, StartDate) : '>' not meaningful for factors | |
#2: In Ops.factor(EpisodeEnd, EndDate) : '<' not meaningful for factors | |
#subseting data set# | |
#subseting data set# | |
dt_march <- df_rename %>% | |
dplyr::filter(Admissions >= "2015-02-05 00:00:00" & Admissions <= "2015-04-30 23:59:00" | | |
Discharges >= "2015-02-05 00:00:00" & Discharges <= "2015-04-30 23:59:00") %>% | |
dplyr::filter(Episode_number == 1) %>% | |
select(IDcol, Admissions, Discharges, Admission_type, Discharge_ward_type, Ward_code, Episode_number) | |
dt_recode <- dt_march %>% | |
mutate(Ward_type = case_when(Admission_type != "Emergency" & Discharge_ward_type == "Other" ~ "Other wards (non_emergency)" , | |
Admission_type == "Emergency" & Discharge_ward_type == "Other" ~ "Other wards (emergency)" , | |
Admission_type != "Emergency" & Discharge_ward_type == "AAU" ~ "AAU" , | |
Admission_type == "Emergency" & Discharge_ward_type == "AAU" ~ "AAU" , | |
Admission_type != "Emergency" & Discharge_ward_type == "Obs Ward" ~ "Obs Ward" , | |
Admission_type == "Emergency" & Discharge_ward_type == "Obs Ward" ~ "Obs Ward" , | |
Discharge_ward_type == "Unknown" ~ "Unknown")) | |
############################ | |
# calculating total time by using difftime - length of stay | |
################################ | |
dt_los <- dt_recode %>% | |
mutate(LOS = as.numeric(difftime(Discharges, Admissions, units = c("days")))) %>% # or minutes? | |
select(IDcol, Admissions, Discharges, Admission_type, Ward_type, LOS, Ward_code) %>% | |
na.omit() | |
#want to create a new column where other ward non-emergency and other ward emergency is captured. | |
# Selecting bins from the Los | |
########################### | |
los <- dt_los$LOS | |
########################## | |
# Need to transform los into numeric | |
########################### | |
as.numeric(los) | |
better_breaks <- c(0, 0.16, 1, | |
2, 3, 4, 7, 21, max(los, na.rm = TRUE)) | |
dt_los$losbinned <- cut(dt_los$LOS, | |
breaks = better_breaks, | |
labels = c("0hrs", "16hrs", | |
"1 d", "2 d", "3 d", | |
"4 - 7 d", "7 - 21 d", "> 21 d"), | |
right = FALSE) | |
df_wrd_c <- dt_los %>% | |
na.omit()%>% | |
group_by(Ward_type, losbinned) %>% | |
dplyr::summarise(Count = n(), | |
CountT = sum(Count), | |
Per = paste0(round(100*Count/CountT,2), '%')) | |
# tried different stuff and did not work | |
# Plotting the lenght of stay by the ward - check for dual axis graphs | |
plot <-ggplot(df_wrd_c, aes(x = losbinned, y = Count, group = Ward_type, fill = Ward_type)) + #alpha= ward.type - this gives a ligther version of the colours I choose # changing the size of the bars for each categ change the parameter width, and in between the stacked categories the white colour is passed on the color command, with the width of the line 0.5 | |
geom_bar(stat = "identity",width = 0.5,colour= "white", lwd = 0.5 ) + | |
scale_fill_brewer(palette= "Oranges") + | |
theme_bw() + | |
#scale_y_continuous(expand = c(0, 0), limits = c(0, 350)) + | |
#scale_y_continuous("Cumulative hospital addmissions, %", sec.axis = ) + | |
labs(title ="Hospital LoS distribution for admitted patients, 1st of March to 31st of March, 2015", | |
subtitle = "Hospital discharges, excl. same-day non-emergency, 1st of March to 31st of March,2015; cummulative hospital LoS in 8 hr bins to 7 d, 7 d bins to28 d, > 28 d | |
Note:(i)LoS calculated in days, incl. trolleyed ED LoS and excl transit areas;(ii) results are intended for management information only", | |
y = "Hospital admissions*, n", x = "Hospital LoS, d ( 16 hr bins to 7d, 7d bins to 21d, >21 d) ", caption = "Source: NIHR")+ | |
theme(axis.title.y = element_text(margin = ggplot2::margin(t = 0, r = 21, b = 0, l = 0)), | |
plot.title = element_text(size = 11, face = "bold"), # changing the parameters in this box will give a different size of the title, with bold passed as a parameter | |
plot.subtitle = element_text(size = 8)) + # changing the parameters in this box will give a different size of the subtitle | |
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1), | |
legend.position = "bottom", legend.box = "horizontal" ) + | |
guides(fill = guide_legend(title = "Discharge from")) | |
plot | |
if(plot_chart == TRUE){ | |
plot | |
}else{ | |
plot$data %>% select(Ward_type, losbinned, Count) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment