Created
January 8, 2019 13:59
-
-
Save gabrielburcea/c9b7f8700f0b04c2b962b7f155f508ab to your computer and use it in GitHub Desktop.
length of stay with stacked bars
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_1_month_with_breaks_provided <- function(data, plot_chart){ | |
# renaming the variables I am interested in | |
# renaming the variables I am interested in | |
df_rename <- data %>% | |
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) | |
colname_ct <- function(data, colname){ | |
data[,colname] = as.POSIXct(data[,colname]) | |
data | |
} | |
#subseting data set# | |
dt_march <- df_rename %>% | |
colname_ct("Admissions") %>% | |
colname_ct("Discharges") %>% | |
dplyr::filter(Admissions >= "2015-02-05 00:00:00" & Admissions <= "2015-05-03 23:59:00" | | |
Discharges >= "2015-02-05 00:00:00" & Discharges <= "2015-05-03 23:59:00") %>% | |
dplyr::filter(Episode_number == 1) %>% | |
mutate( | |
Admitted_date = as.Date(Admissions), | |
Discharge_date = as.Date(Discharges), | |
Same_day_discharge = ifelse(Admitted_date == Discharge_date, "True", "False"), | |
Maternity = ifelse(Admission_type == "Maternity" & Discharge_ward_type == "Unknown", "Maternity", "Unknown") | |
) %>% | |
select(IDcol, Admissions, Discharges, Admission_type, Discharge_ward_type, Ward_code, Episode_number, Admitted_date, Discharge_date, Same_day_discharge, Maternity) | |
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" , | |
Admission_type == "Maternity" & Discharge_ward_type == "Unknown" ~ "Maternity")) | |
############################ | |
# calculating total time by using difftime - length of stay | |
# getting rid of the same day non-emergency | |
################################ | |
dt_los <- dt_recode %>% | |
filter(Same_day_discharge == "False") %>% | |
mutate(LOS = as.numeric(difftime(Discharges, Admissions, units = c("days")))) %>% # or minutes? | |
select(IDcol, Admissions, Discharges, Admission_type, LOS, Ward_code, Same_day_discharge, Ward_type) %>% | |
na.omit() | |
# Selecting bins from the Los | |
########################### | |
los <- dt_los$LOS | |
########################## | |
# Need to transform los into numeric | |
########################### | |
as.numeric(los) | |
########################### | |
# choosing the bins | |
########################## | |
breaks <- c(0, 0.8, 0.16, | |
1, 1.8, 1.16, | |
2, 2.8, 2.16, | |
3, 3.8, 3.16, | |
4, 4.8, 4.16, | |
5, 5.8, 5.16, | |
6, 6.8, 6.16, | |
7, 14, 21, 28, max(los, na.rm = TRUE)) | |
########################### | |
dt_los$losbinned <- cut(dt_los$LOS, | |
breaks = breaks, | |
labels = c("0hrs", "8hrs", "16hrs", | |
"1 d", "1 d 8hrs", "1 d 16hrs", | |
"2 d", "2 d 8hrs", "2 d 16hrs", | |
"3 d", "3 d 8hrs", "3 d 16hrs", | |
"4 d", "4 d 8hrs", "4 d 16hrs", | |
"5 d", "5 d 8hrs", "5 d 16hrs", | |
"6 d", "6 d 8hrs","6 d 16hrs", | |
"7 - 14 d", "15 - 21 d", "22 - 28 d", "> 28 d"), | |
right = TRUE) | |
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, lwd = 0.5, colour = "white") + # | |
scale_fill_brewer(palette= "Reds") + | |
scale_y_continuous(expand = c(0, .9)) + | |
theme_bw() + | |
#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 (8 hr bins to 7d, 7d bins to 28d, >28 d) ", caption = "Source: CLAHRC NWL")+ | |
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