Skip to content

Instantly share code, notes, and snippets.

@gabrielburcea
Created December 17, 2018 18:10
Show Gist options
  • Save gabrielburcea/d19b589ec09ecdc18a298370e2daac28 to your computer and use it in GitHub Desktop.
Save gabrielburcea/d19b589ec09ecdc18a298370e2daac28 to your computer and use it in GitHub Desktop.
Attend_Adm
# renaming the variables I am interested in
df_rename <- df %>%
select(PAT_CODE, START_DATETIME, END_DATETIME, episode.order, AGE_BAND, SEX, spell.type, WARD_CODE, spell.end.ward) %>%
dplyr::rename(IDcol = PAT_CODE, Admissions = START_DATETIME, PatientType = spell.type,
Discharges = END_DATETIME, EpisodeNumber = episode.order, Ward = WARD_CODE,
Gender = SEX, AgeBand = AGE_BAND, LastWard = spell.end.ward)
#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#
dt_march_apr <- df_rename %>%
#colname_ct("Admissions") %>%
#colname_ct("Discharges") %>%
dplyr::filter(Admissions >= "2015-03-01 00:00:00" & Admissions <= "2015-04-27 23:59:00" |
Discharges >= "2015-03-01 00:00:00" & Discharges <= "2015-04-27 23:59:00") %>%
select(IDcol, Admissions, Discharges, PatientType, Gender, AgeBand, Ward, LastWard, EpisodeNumber)
# finding the number of ae attendances
df_ae_attendances <- dt_march_apr %>%
filter(Ward == "A&E" & Gender != "Not Specified") %>%
filter(EpisodeNumber == 1) %>%
group_by(Gender, AgeBand) %>%
dplyr::summarize(Attend = n()) %>%
mutate(Attendances_Gender = case_when(Gender == "Female" ~ "Female not admitted",
Gender == "Male" ~ "Male not admitted"))
df_ae_admissions <- dt_march_apr %>%
filter(Ward == "A&E" & LastWard == "ED only") %>%
filter(Gender != "Not Specified" & EpisodeNumber == 1) %>%
group_by(Gender, AgeBand) %>%
dplyr::summarize(Admitted = n()) %>%
mutate(Admissions_Gender = case_when(Gender == "Female" ~ "Female admitted",
Gender == "Male" ~ "Male admitted"))
# plot the two dataset
plot <- ggplot() +
geom_bar(data = df_ae_attendances , aes(x = AgeBand, fill = Attendances_Gender, y = Attend),
stat = 'identity', position = 'dodge') +
geom_bar(data = df_ae_admissions, aes(x = AgeBand, fill = Admissions_Gender, y = Admitted),
stat = 'identity', position = position_dodge(0.9), width = 0.6) +
xlim("0 yrs" , "1-4 yrs", "5-9 yrs", "10-14 yrs", "15-19 yrs", "20-24 yrs", "25-29 yrs",
"30-34 yrs", "35-39 yrs", "40-44 yrs", "45-49 yrs", "50-54 yrs", "55-59 yrs",
"60-64 yrs", "65-69 yrs", "70-74 yrs", "75-79 yrs", "80-84 yrs", "85+ yrs") +
xlab("Age Group") +
ylab("ED Attendances and Admissions") +
labs(title = "Chelsea & Westminster: A&E Attendances and Admissions, year ending 2015",
subtitle = "ED attendance and hospital admissions from ED, 2011-2015, n
Note: (i) planned return/recall attendances have been excluded; (ii) results are intended for management information only",
y = "Average occupancy, n", x = "Age Group", caption = "Source: CLAHRC NWL") +
theme_bw() +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 21, b = 0, l = 0)),
plot.title = element_text(size = 12, face = "bold"),
plot.subtitle = element_text(size = 10),
legend.position = "bottom", legend.box = "horizontal") +
scale_y_continuous(expand = c(0, .5)) +
scale_fill_manual("", values = c("coral3", "lightcoral",
"steelblue4", "lightblue2"))
# Things to consider - StartDate and EndDate does not do what it should do -> it plots all the date from 2011-2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment