ddediu / AdhereR

Computation of adherence to medications from Electronic Healthcare Data in R
26 stars 6 forks source link

Maybe show the legend in a floating HTML element/outside the plot in Shiny? Also for basic plotting? #156

Open ddediu opened 3 years ago

ddediu commented 3 years ago

This would avoid crowding of the shiny plot and be better from an UI perspective

ddediu commented 3 years ago

See also issue #101

ddediu commented 2 years ago

Might also be useful for normal plotting (Suggested by Tsaone Tamuhla)

ddediu commented 2 years ago

This turns out to be pretty much and might require some serious changes to the code. However, as a workaround, there's now a parameter do.not.draw.plotto the plot()function which is intended to only plot the legend (at coordinates (0,0)), which, combined with get.legend.plotting.area() should allow the placement of the legend outside the CMA plot

mumbarkar commented 1 year ago

Hello @ddediu,

Please find below R code to plot cma0 plot and legends separate. I think this may be helpful for other researchers.

Thanks in advance...

load the required libraries

library(AdhereR) library(dplyr)

Load the built-in dataset "med.events":

df <- med.events %>% filter(PATIENT_ID %in% c(37,76))

Take a look into data

head(med.events)

Create a CMA0 object to generate patient history

cma0 <- CMA0(data=df, # use the two selected patients ID.colname="PATIENT_ID", # the name of the column containing the IDs event.date.colname="DATE", # the name of the column containing the event date event.duration.colname="DURATION", # the name of the column containing the duration event.daily.dose.colname="PERDAY", # the name of the column containing the dosage medication.class.colname="CATEGORY", # the name of the column containing the category followup.window.start=0, # FUW start in days since earliest event observation.window.start=182, # OW start in days since earliest event observation.window.duration=365, # OW duration in days date.format="%m/%d/%Y"); # date format (mm/dd/yyyy)

plot the treatment patterns

1. Create a 1 x 2 plotting matrix

par(mfrow = c(1,2))

2. create only cma0 plot

plot(cma0, # the object to plot align.all.patients = TRUE, # align all patients for easier comparison patients.to.plot = NULL, # a vector of string containing list of patients to be plot duration = 365*2, # a number, total duration (in days) of the whole period to plot align.first.event.at.zero = FALSE, # Logical,the first event be placed at the origin of the time axis? show.period = c("dates","days")[2], # show period in dates or days (if align.all.patients == TRUE then plot will show x axis in days only) period.in.days = 30, # the number of days at which the regular grid is drawn show.legend = FALSE, # Logical, should the legend be drawn? bw.plot = FALSE)

3. add legends only

plot(cma0, # the object to plot align.all.patients = TRUE, # align all patients for easier comparison patients.to.plot = NULL, # a vector of string containing list of patients to be plot duration = 365*2, # a number, total duration (in days) of the whole period to plot align.first.event.at.zero = FALSE, # Logical,the first event be placed at the origin of the time axis? show.period = c("dates","days")[2], # show period in dates or days (if align.all.patients == TRUE then plot will show x axis in days only) period.in.days = 30, # the number of days at which the regular grid is drawn show.legend = TRUE, # Logical, should the legend be drawn? bw.plot = FALSE, legend.x = "right", legend.y = "top", legend.bkg.opacity = 0.5, legend.cex = 0.35, legend.cex.title = 1, cex = 1, cex.axis = 0.45, do.not.draw.plot = TRUE) # logical, plot only legends if show.legend == TRUE

Output:

image