[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Mar 22 22:11:43 CET 2012
On Thu, Mar 22, 2012 at 4:47 PM, Paul Miller <pjmiller_57 at yahoo.com> wrote:
> Hi Michael,
>
> Made some progress but not there yet.
>
> Got point #1 by creating an ordered factor. There doesn't appear to be a command called Scale_x_flip().
>
Sorry, my mistake: I meant scale_x_reverse() ...
> Still not sure how to decrease vertical space between the line segments for each drug. Thought this would be necessary in order to get a fair number of graphs onto a page in Word or in a pdf (say 4-5). I had imagined creating graphs that would be wider than they are tall in order to accomplish this.
You can set the size of the image device directly, e.g., png(....,
width = 400, height = 200) and then make your plots one per image,
closing the device with dev.off() when you're done. I think getting
multiple plots per page (without facetting) won't be super easy and
requires a few tricks with grid tools; someone might know better.
I know pdf() supports multiple pages if you plot multiple graphs and
don't close the device between, but I'm not sure that's doable with
the other ones.
>
> Removed numbering from the x-axis using "opts(axis.text.x = theme_blank()". This is slightly different from the "opts(axis.ticks.x = theme_blank())" that you suggested. Having axis.ticks.x made it a lot easier to figure out I needed axis.text.x though.
>
> Got the text for pattern at the top (point #4).
>
> Still struggling with the text for patient and line (point #5). Managed to at least add some text manually to show what the text would look like for one of the graphs.
I'm not sure what you are looking for here -- is there an image online
you can point me too?
>
> Not sure how to get a separate graph for each patient x line combination. Discovered facets earlier today. Think this will just give me one giant graph though that contains graph for each patient x line combination.
> This isn't so bad but it's not clear how one could print it and have the graphs break properly across pages.
>
As described above, you probably want to make multiple files by
looping over your data and selecting subsets. I know Bert pointed you
to xyplot in lattice graphics -- if you get that working, I believe it
has some natural support for multi-page graphics but I don't know the
package so well.
> If you can provide any additional help, that's great. If not that's OK too. As I said earlier, there's a big time crunch around this. If I can't figure it out today, then we'll just move forward without it. Then I can figure out how to do it later for my own edification and at my own leisure. Chances are that will be a lot more fun than trying to learn under intense time pressure.
>
> Thanks,
>
> Paul
>
>
> connection <- textConnection("
> 1/1/Drug A/ Begin (A), Begin (B), End (B), End (A)/0.0000/21.000
> 1/1/Drug B/ Begin (A), Begin (B), End (B), End (A)/0.7143/18.000
> 1/2/Drug A/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000
> 1/2/Drug B/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000
> 1/2/Drug C/ Begin (A, B, C), End (A, B), End (C)/0.0000/36.000
> 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
>
> TestData$drug <- factor(TestData$drug, levels = c("Drug D", "Drug C", "Drug B", "Drug A"))
>
> ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("") + ylab("") + theme_bw() +
> opts(title = "Pattern = Begin (A), Begin (B), End (B), End (A) \n (profile_key = 2, line = 1)") +
> opts(axis.text.x = theme_blank() )
>
>
>
> --- On Thu, 3/22/12, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:
>
>> From: R. Michael Weylandt <michael.weylandt at gmail.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:55 PM
>> Inline:
>>
>> On Thu, Mar 22, 2012 at 1:41 PM, Paul Miller <pjmiller_57 at yahoo.com>
>> wrote:
>> > 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.
>>
>> Scale_x_flip()
>>
>> >
>> > 2. Decrease the vertical space between the line
>> segments for each drug so they are fairly close to one
>> another.
>>
>> Why? This will happen automatically if needed....
>>
>> >
>> > 3. Remove the numbering from the x-axis.
>>
>> opts(axis.ticks.x = theme_blank())
>>
>> >
>> > 4. Put the text for pattern above the graph (e.g.,
>> "Begin (A), Begin (B), End (B), End (A)"), either centered
>> or left aligned.
>>
>>
>> opts(title = "Begin (A), Begin (B), End (B), End (A)")
>>
>> >
>> > 5. Put the patient and line information below the text
>> for pattern (e.g., "profile_key = 1, line = 1")
>>
>> example(geom_text)
>> or
>> https://learnr.wordpress.com/2010/01/03/directlabels-adding-direct-labels-to-ggplot2-and-lattice-plots/
>>
>> >
>> > 6. Output a separate graph for each patient and line of
>> treatent.
>> >
>> > Ultimately, I want to combine all the graphs into a
>> single Word document. Or perhaps better yet, to create a pdf
>> using LaTeX.
>>
>> Google Sweave or (possibly easier) knitr
>>
>> >
>> > I'm going to continue to try and fugure this out as
>> best I can. Any help with it will be greatly appreciated
>> though.
>> >
>> > Thanks,
>> >
>> > Paul
>> >
>> >
>> > connection <- textConnection("
>> > 1/1/Drug A/ Begin (A), Begin (B), End (B), End
>> (A)/0.0000/21.000
>> > 1/1/Drug B/ Begin (A), Begin (B), End (B), End
>> (A)/0.7143/18.000
>> > 1/2/Drug A/ Begin (A, B, C), End (A, B), End
>> (C)/0.0000/20.000
>> > 1/2/Drug B/ Begin (A, B, C), End (A, B), End
>> (C)/0.0000/20.000
>> > 1/2/Drug C/ Begin (A, B, C), End (A, B), End
>> (C)/0.0000/36.000
>> > 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()
>>
>> It would be easier if you could use dput() but this was
>> helpfully reproducible.
>>
>> Hope this gets you started,
>>
>> Michael
>>
>> > 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.
>>
More information about the R-help
mailing list