[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
pjmiller_57 at yahoo.com
Thu Mar 22 19:20:46 CET 2012
Seems I've not provided enough detail. Sometimes it's hard to know how much to put in.
If you run all the code I sent, I think my data do make it into the "long" format.
Let me begin by trying to explain my data as they appear prior to the melt() function. The data for patient 2 (profile_key = patient) and line 1 of treatment look like:
profile_key line drug pattern start_drug stop_drug
6 2 1 Drug A Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D) 0.0000 7.429
7 2 1 Drug B Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D) 0.0000 7.429
8 2 1 Drug C Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D) 14.5714 21.857
9 2 1 Drug D Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D) 25.4286 231.286
Here we have a cancer patient who took 4 drugs within a line of treatment. Their treatment involved the following sequence:
Start Drug A and Drug B
Stop Drug A and Drug B
Start Drug C
Stop Drug C
Start Drug D
Stop Drug D
This is reflected in their value for pattern:
Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)
The patient has separate rows for each drug (A-D) and also start_drug and stop_drug variables that indicate when the drugs started and stopped relative to one another. The start_drug and stop_drug variables are measurements in weeks. I don't actually want the time scale to appear on the x-axis though. I could go into why but think that might introduce too much detail rather than too little.
If you look at the code from "reshape()" on down, I think you'll see that the data do get transposed into long form.
The ggplot code also produces the beginning of a very nice graph. I should say that so far I really like ggplot2. I think it makes very pretty graphs.
I just want to know how to alter the code to produce the changes I described earlier.
Do you know how to make those changes? Or can anyone else show me how to make them?
--- On Thu, 3/22/12, Bert Gunter <gunter.berton at gene.com> wrote:
> From: Bert Gunter <gunter.berton at gene.com>
> Subject: Re: [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
> To: "Paul Miller" <pjmiller_57 at yahoo.com>
> Cc: r-help at r-project.org
> Received: Thursday, March 22, 2012, 12:50 PM
> Your data are still uninterpretable
> to me.
> A general approach to this sort of thing is to get your data
> in "long"
> format, like this
> patientID time value
> On Thu, Mar 22, 2012 at 10:41 AM, Paul Miller <pjmiller_57 at yahoo.com>
> > Hello All,
> > Want very much to learn how to plot patient drug
> timelines. Trouble is I need to figure out how to do this
> today. So not much time for me to struggle with it. Hoping
> someone can just help me out a bit.
> > Below are some sample data and code that produces what
> I think is the beginning of a very nice graph.
> > Need to alter the code to:
> > 1. Get the lines for the drugs to appear on the y-axis
> in the order that they appear in the data.
> > 2. Decrease the vertical space between the line
> segments for each drug so they are fairly close to one
> > 3. Remove the numbering from the x-axis.
> > 4. Put the text for pattern above the graph (e.g.,
> "Begin (A), Begin (B), End (B), End (A)"), either centered
> or left aligned.
> > 5. Put the patient and line information below the text
> for pattern (e.g., "profile_key = 1, line = 1")
> > 6. Output a separate graph for each patient and line of
> > Ultimately, I want to combine all the graphs into a
> single Word document. Or perhaps better yet, to create a pdf
> using LaTeX.
> > I'm going to continue to try and fugure this out as
> best I can. Any help with it will be greatly appreciated
> > Thanks,
> > Paul
> > connection <- textConnection("
> > 1/1/Drug A/ Begin (A), Begin (B), End (B), End
> > 1/1/Drug B/ Begin (A), Begin (B), End (B), End
> > 1/2/Drug A/ Begin (A, B, C), End (A, B), End
> > 1/2/Drug B/ Begin (A, B, C), End (A, B), End
> > 1/2/Drug C/ Begin (A, B, C), End (A, B), End
> > 2/1/Drug A/ Begin (A, B), End (A, B), Begin (C), End
> (C), Begin (D), End (D)/0.0000/7.429
> > 2/1/Drug B/ Begin (A, B), End (A, B), Begin (C), End
> (C), Begin (D), End (D)/ 0.0000/7.429
> > 2/1/Drug C/ Begin (A, B), End (A, B), Begin (C), End
> (C), Begin (D), End (D)/ 14.5714/21.857
> > 2/1/Drug D/ Begin (A, B), End (A, B), Begin (C), End
> (C), Begin (D), End (D)/ 25.4286/231.286
> > 2/2/Drug A/ Begin (A, B), End (A, B)/0.0000/35.286
> > 2/2/Drug B/ Begin (A, B), End (A, B)/0.0000/35.286
> > ")
> > TestData <- data.frame(scan(connection,
> list(profile_key=0, line=0, drug="", pattern="",
> start_drug=0, stop_drug=0), sep="/"))
> > TestData <- TestData[TestData$profile_key == 2 &
> TestData$line == 1,]
> > TestData
> > require(reshape)
> > TestData <- melt(TestData, measure.vars =
> c("start_drug", "stop_drug"))
> > TestData
> > ggplot(TestData, aes(value, drug)) + geom_line(size =
> 6) + xlab("") + ylab("") + theme_bw()
> > ggsave(file = "plot21.pdf")
> > ______________________________________________
> > R-help at r-project.org
> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> Bert Gunter
> Genentech Nonclinical Biostatistics
> Internal Contact Info:
> Phone: 467-7374
More information about the R-help