I'm trying to plot how a single drug has been prescribed in the hospital. In this dummy database I have 1000 patient encounters after 2017/01/01.
The goal of plotting is to see the pattern of administration of this drug: Is it given more frequently / high dose closer to time of admission, discharge, or in the middle of patient stay.
#Get_random_dates that we will use multiple times
gen_random_dates <- function(N, st, et) {
st <- as.POSIXct(as.Date(st))
et <- as.POSIXct(as.Date(et))
dt <- as.numeric(difftime(et,st,unit="sec"))
ev <- runif(N, 0, dt)
rt <- st + ev
return(rt)
}
#Generate admission and discharge dates
admission <- gen_random_dates(1000, "2017/01/01", "2017/01/10")
discharge <- gen_random_dates(1000, "2017/01/11", "2017/01/20")
patient <- sort(sample(1:1000, 1000))
patient_data <- data.frame(patient_ID = patient, admission_date = admission, discharge_date = discharge)
#Grow the database
patient_data <- patient_data[sort(sample(1000, 100000, replace=TRUE)), ]
#Medication admin date and dose
patient_data$admin_date <- gen_random_dates(100000, patient_data$admission_date, patient_data$discharge_date)
patient_data$admin_dose <- abs(as.integer(rnorm(100000, 50, 100)))
I tried this ggplot function but it did not help me visualize the pattern.
ggplot(patient_data, aes(x = admin_date, y = admin_dose)) +
xlab("Use of Drug in Patient Encounters") + ylab("Dose (mg)") +
geom_jitter()
ggplot