ddediu / AdhereR

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

Request for help: How to customize x axis using plot (CMA) #99

Closed joliveiracosta closed 5 years ago

joliveiracosta commented 5 years ago

Hi ddediu, How can I customize the length of the x axis for the plot(CMA)? I have been trying just adding (xlim = c(0,450)) to the plot(CMA) function and it is not working. Thanks!

ddediu commented 5 years ago

Hi Juliana,

very good question, but first, why would you need to do so and in what context?

Normally, the x axis is computed automatically depending on the Follow-Up Window (FUW) you define for your patient(s) (and if they are aligned or not), so the most straightforward mode to change it would be to enlarge/shrink the FUW in the CMA() computation, I guess?

Best, Dan

ddediu commented 5 years ago

Alternatively, if there's a good case to be made, I could implement a sort of xlim but that will depend on details of the plotting such as the FUW(s) of the patient(s) plotted, if they are aligned or not, if the alignment is to 0 or not, and if one plots actual dates or days since first plotted event -- this would make a generic way of specifying xlim pretty ugly :(

Best, Dan

joliveiracosta commented 5 years ago

Hi Dan, So I have tried to change the FUW, and it does not change the plot! I thought it was because I was trying to plot for one specific patient, but than I ran the CMA just for that patient and still it does not change! What I am trying to do is to adjust the following figure to show just a little bit further than the follow up (FUW = 360 days; figure should be up to 450 days). I was also thinking about putting the legend next to the figure, instead of inside it.

plot_to_adjust .

ExamplePat_to_plot.xlsx

ExamplePats2 <- read_excel("ExamplePat_to_plot.xlsx") ExamplePats2$SUPP_DATE_char2 <- as.character(ExamplePats2$SUPP_DATE_ADJ)

cma5a <- CMA5(data=ExamplePats2, # ID.colname="PAT_ID", event.date.colname="SUPP_DATE_char2", event.duration.colname="duration", medication.class.colname="drug_name", carry.only.for.same.medication=FALSE, # carry-over across medication types consider.dosage.change=FALSE, # don't consider changes in dosage followup.window.start=0, observation.window.start=0, observation.window.duration=360, date.format="%Y-%m-%d");

plot(cma5a, show.legend=TRUE, observation.window.density = 50, period.in.days = 30, highlight.followup.window = FALSE, print.CMA =FALSE,plot.CMA = FALSE, legend.y = "bottom",legend.x = 360, legend.cex = 0.75, show.cma=FALSE, observation.window.col = "gray80", legend.bkg.opacity = 1)

ddediu commented 5 years ago

Hi Juliana,

so, when I run your code, if I add followup.window.duration=450, to the CMA5(...) call, the plot goes up to 450 days.

On the other hand, the legend is much too wide (I'll debug it), but coming back to you suggestion:

I was also thinking about putting the legend next to the figure, instead of inside it.

the "standard" basic plot way of showing the legend in inside the plot while ggplot has a different approach. Now, I think what I could do is to allow the legend to be placed the right of the plot (i.e., outside the plot; I would rather not allow it to be moved around the plot as it increases the complexity of the code) -- would that answer your needs?

Best, Dan

ddediu commented 5 years ago

Hi again,

also, I just remembered that there's a parameter for plot() named duration:

duration A number, the total duration (in days) of the whole period to plot; in NA it is automatically determined from the event data such that the whole dataset fits.

which should do exactly what you need (without fiddling with FUW) and it works :)

(The legend is so big because of the legend.cex).

So, I guess what would remain to be done is plotting the legend outside the plot but this would require even more horizontal space (now I remembered why I didn't offer this option!) -- if you think this is an important use-case, I will discuss it with Alex and Sam and come to a decision...

Best, Dan

joliveiracosta commented 5 years ago

Hi Dan, Thank you for all the assistance! After adjusting the duration and the size of the legend, the plot looks nice! I am happy with it the way it is. The legend outside the plot was just a suggestion, I don't think it is a necessary adjustment to make. Best, Juliana

ddediu commented 5 years ago

Hey Juliana,

glad it solved your problem! I'll think about the legend outside (but I have quite a few high-priority things to do right now, so it might have to wait given that it's not a must for you)

Best, Dan