[R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!
Paul Miller
pjmiller_57 at yahoo.com
Thu Mar 22 21:47:48 CET 2012
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().
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.
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.
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.
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