Paul Miller

2012-Mar-22 17:41 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

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 another. 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 treatent. 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 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() ggsave(file = "plot21.pdf")

Bert Gunter

2012-Mar-22 17:50 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

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 A On Thu, Mar 22, 2012 at 10:41 AM, 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. > > 2. Decrease the vertical space between the line segments for each drug so they are fairly close to one another. > > 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 treatent. > > 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 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() > 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 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

R. Michael Weylandt

2012-Mar-22 17:55 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

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.

Bert Gunter

2012-Mar-22 17:58 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

The message apparently sent itself before I was ready . :-( Here''s the full version. I hope I have interpreted your intent correctly. Ignore if I have not. A general approach to this sort of thing is to get your data in "long" format, like this patientID ? time ? value ?A 1 .029 A 2 .053 . . . B 1 1.25 . . . The ID''s do not need to appear in order or together. The times need not be in order. Once the data are in this form, xyplot() in lattice can do it via: xyplot( value ~ time , group = patientID, type = "c("l","p"), auto.key=TRUE ) ?xyplot for additional info. -- Bert> > On Thu, Mar 22, 2012 at 10:41 AM, 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. >> >> 2. Decrease the vertical space between the line segments for each drug so they are fairly close to one another. >> >> 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 treatent. >> >> 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 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() >> 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 > Website: > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

Paul Miller

2012-Mar-22 18:20 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Bert, 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? Thanks, Paul --- 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 > ? A > > On Thu, Mar 22, 2012 at 10:41 AM, 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. > > > > 2. Decrease the vertical space between the line > segments for each drug so they are fairly close to one > another. > > > > 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 > treatent. > > > > 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 > 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() > > 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 > Website: > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm >

Paul Miller

2012-Mar-22 20:47 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

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. >

R. Michael Weylandt

2012-Mar-22 21:11 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

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. >>

Paul Miller

2012-Mar-22 21:48 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Michael, Want to reverse the ordering of drugs on the y-axis. So tried scale_y_reverse() instead of scale_x_reverse(). Sure enough, the command exits. Unfortunately, it is "unsuitable for factors". I tried converting "drug" to character but this didn''t work. Suspect scale_x_reverse() only works for something numeric which I don''t think is helpful in my case. So changing "drug" to an ordered factor may make sense. I found some stuff online suggesting this approach. The suggestion for setting the size of the image device directly was very helpful. My code now looks like: 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")) png(filename="plot21.png", width=600, height=300) 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() ) dev.off() This produces a perfectly sized image. I imported the image created into Word and it looks like I would get 3 images per page. As far as the text at the top of the graphs goes, I have 4 patient x line combinations in my data (at least before I filter to get a single patient by line combination). So the text at the top of each graph (without any filtering) would be as follows: Pattern = Begin (A), Begin (B), End (B), End (A) (profile_key = 1, line = 1) Pattern = Begin (A, B, C), End (A, B), End (C) (profile_key = 1, line = 2) Pattern = Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D) (profile_key = 2, line = 1) Pattern = Begin (A, B), End (A, B) (profile_key = 2, line = 2) As far as I know, there''s nothing like this online. I hope this makes it clear though. The last part would be looping. I think I could figure out how to do this using a for loop or some other means if I can get one of the graphs set up correctly. The graphs themselves might be called something like plot11, plot12, plot21, and plot22. Thanks, Paul --- 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, 4:11 PM > 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. > >> >

Paul Miller

2012-Mar-22 22:59 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Michael, This may be an ugly way of doing things but the "title = unique(paste( ..." bit in the code below automates the code for pattern, patient, and line. So now all I''d need is the looping part. If good ways of automating the code for pattern, patient, and line or of doing the looping are apparent, please let me know. Thanks, Paul ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("") + ylab("") + theme_bw() + opts(title = unique(paste(TestData$pattern, "\n (profile_key = ", TestData$profile_key, ", line = ", TestData$line, ") \n" ) ) ) + opts(axis.text.x = theme_blank() )

Dear R users, Let f be a function over d variables x1,..,xd. I want to compute the k^th-order derivative with respect to x1,..,xk (k<=d). I have a by hand solution (see below) using an iterating code using D. However, I expect d to be high and f to be complicated. Then I want a vector x to be the input, instead of x1,..,xd. How to avoid the x1 <- x[1]; x2 <- x[2], etc steps in the code below? Moreover, D uses symbolic differentation and then eval evaluates the output to get a numerical result. But is there a way to compute the desired derivatives numerically directly (without using symbolic calculus at all)? Finally, what is the most efficient and fast way to get a numerical result for such derivatives? Thank you very much in advance, Gildas ### Code ### ### dif takes a function f, an order k, and a vector x as input. f must be a function of x1,..,xd with d >= k. The correspondance is done between xi and x[i]. The expression for f must be at the last row of the body function. dif <- function(f,k,x){ o <- list() n <- length(body(f)) o[[1]] <- body(f)[[n]] for (i in 1:k){ xi <- paste("x",i,sep="") o[[i+1]] <- D(o[[i]],name=xi) } x1 <- x[1] x2 <- x[2] x3 <- x[3] eval(o[[k+1]]) } ### Examples ### ## function to differentiate f <- function(x){ x1 <- x[1] x2 <- x[2] x3 <- x[3] 0.5*x1*x2*x3^2 } ## derivative w.r.t. x1, x2 and x3 at the point (1,2,3). dif(f,3,c(1,2,3)) ### My Questions ### ## how to avoid to write by hand xi <- x[i] ?? ## is there a way in R to compute such derivatives without using symbolic calculation but numerical compuation instead.

On Fri, Mar 23, 2012 at 12:35:57AM +0100, Gildas Mazo wrote:> Dear R users, > > Let f be a function over d variables x1,..,xd. I want to compute the k^th-order derivative with respect to x1,..,xk (k<=d). I have a by hand solution (see below) using an iterating code using D. However, I expect d to be high and f to be complicated. Then I want a vector x to be the input, instead of x1,..,xd. How to avoid the x1 <- x[1]; x2 <- x[2], etc steps in the code below? Moreover, D uses symbolic differentation and then eval evaluates the output to get a numerical result. But is there a way to compute the desired derivatives numerically directly (without using symbolic calculus at all)? Finally, what is the most efficient and fast way to get a numerical result for such derivatives? > > Thank you very much in advance, > Gildas > > ### Code ### > ### dif takes a function f, an order k, and a vector x as input. f must be a function of x1,..,xd with d >= k. The correspondance is done between xi and x[i]. The expression for f must be at the last row of the body function. > dif <- function(f,k,x){ > o <- list() > n <- length(body(f)) > o[[1]] <- body(f)[[n]] > for (i in 1:k){ > xi <- paste("x",i,sep="") > o[[i+1]] <- D(o[[i]],name=xi) > } > x1 <- x[1] > x2 <- x[2] > x3 <- x[3] > eval(o[[k+1]]) > } > > ### Examples ### > ## function to differentiate > f <- function(x){ > x1 <- x[1] > x2 <- x[2] > x3 <- x[3] > 0.5*x1*x2*x3^2 > } > ## derivative w.r.t. x1, x2 and x3 at the point (1,2,3). > dif(f,3,c(1,2,3)) > > ### My Questions ### > ## how to avoid to write by hand xi <- x[i] ?? > ## is there a way in R to compute such derivatives without using symbolic calculation but numerical compuation instead.Hi. For the first question, try the following dif <- function(f,k,x){ o <- list() n <- length(body(f)) o[[1]] <- body(f)[[n]] for (i in 1:k){ xi <- paste("x",i,sep="") o[[i+1]] <- D(o[[i]],name=xi) assign(xi, x[i]) } eval(o[[k+1]]) } For the second question, try the following. x <- c(1, 2, 3) k <- length(x) grid <- as.matrix(expand.grid(rep(list(c(0, 1)), times=k))) signs <- 1 - 2*(rowSums(1 - grid) %% 2) for (eps in 2^-(5:20)) { xeps <- eps*grid + rep(x, each=nrow(grid)) print(sum(signs*apply(xeps, 1, FUN=f))/eps^k) } [1] 3.015625 [1] 3.007812 [1] 3.003906 [1] 3.001953 [1] 3.000977 [1] 3.000488 [1] 3.000244 [1] 3.000122 [1] 3 [1] 3 [1] 3 [1] 3 [1] 4 [1] 0 [1] 0 [1] 0 If the above is computed in an exact arithmetic, then with "eps" converging to zero, the result converges to the required derivative. Since the numerical computations are done with a rounding error, too small "eps" yields a completely wrong result. The choice of a good "eps" depends on the function and on "k". For a high "k", there may even be no good "eps". See the considerations at http://en.wikipedia.org/wiki/Numerical_derivative where the choice of "eps" is discussed in the simplest case of a univariate function. Hope this helps. Petr Savicky.

Paul Miller

2012-Mar-23 13:40 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Michael, Added a little more to my code (see below). It now automatically sets the name of the file. It also does a better job of spacing the text for pattern and patient x line at the top of the graph. I really like the way this looks now. I just need to figure out how to loop through the data using my "key_line" (patient x line) variable. One of the things I''ve noticed while learning R is that things I think will be difficult often go surprisingly well. It''s the things that I think will be easy that I wind up struggling with. Right now I''m struggling with figuring out how to loop through the data to produce plot11, plot 12, plot21, and plot22. Embarassing. But there it is. Can you show me how to do that? In the meantime, I keep working on it and may figure it out on my own. 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 == 1 & TestData$line == 1,] TestData require(reshape) TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) TestData$drug <- factor(TestData$drug, levels = c("Drug D", "Drug C", "Drug B", "Drug A")) TestData$key_line <- with(TestData,paste(profile_key, line, sep = "")) TestData require(ggplot2) png(filename = paste("plot", unique(TestData$key_line), ".png", sep = ""), width=600, height=300) ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + opts(title = paste("Pattern = ", unique(TestData$pattern), " \n (profile_key = ", unique(TestData$profile_key), ", line = ", unique(TestData$line), ") \n", sep = "")) + opts(axis.text.x = theme_blank() ) dev.off()

R. Michael Weylandt

2012-Mar-23 13:52 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Inline. On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hi Michael, > > Added a little more to my code (see below). It now automatically sets the name of the file. It also does a better job of spacing the text for pattern and patient x line at the top of the graph. > > I really like the way this looks now. I just need to figure out how to loop through the data using my "key_line" (patient x line) variable. > > One of the things I''ve noticed while learning R is that things I think will be difficult often go surprisingly well. It''s the things that I think will be easy that I wind up struggling with. Right now I''m struggling with figuring out how to loop through the data to produce plot11, plot 12, plot21, and plot22. > > Embarassing. But there it is. > > Can you show me how to do that? In the meantime, I keep working on it and may figure it out on my own. > > 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 == 1 & TestData$line == 1,] > TestData > > require(reshape) > TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) > TestData$drug <- factor(TestData$drug, levels = c("Drug D", "Drug C", "Drug B", "Drug A")) > TestData$key_line <- with(TestData,paste(profile_key, line, sep = "")) > TestDataUseful trick: if you use dput() you can send this all in a much more concise fashion: structure(list(profile_key = c(1, 1, 1, 1), line = c(1, 1, 1, 1), drug = structure(c(4L, 3L, 4L, 3L), .Label = c("Drug D", "Drug C", "Drug B", "Drug A"), class = "factor"), pattern = structure(c(4L, 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, B), End (C)", " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)", " Begin (A), Begin (B), End (B), End (A)"), class = "factor"), variable = structure(c(1L, 1L, 2L, 2L), .Label = c("start_drug", "stop_drug"), class = "factor"), value = c(0, 0.7143, 21, 18), key_line = c("11", "11", "11", "11")), .Names = c("profile_key", "line", "drug", "pattern", "variable", "value", "key_line"), row.names = c(NA, -4L), class = "data.frame")> > require(ggplot2) > > png(filename = paste("plot", unique(TestData$key_line), ".png", sep = ""), width=600, height=300) > > ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + > ? ? ? ? ? ? ? ? opts(title = paste("Pattern = ", unique(TestData$pattern), " \n (profile_key = ", unique(TestData$profile_key), ", line = ", unique(TestData$line), ") \n", sep = "")) + > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x = theme_blank() ?) > > dev.off() > >If you want to loop over the different values of "key_line", I think it''s pretty easy: TempData <- split(TestData, TestData$keyline) # List of data frames for(temp in TempData){ # Loop over the list ## Do all your stuff -- just change "TestData" to "temp" so you are using the right data.frame } Hope this helps, Michael

Petr PIKAL

2012-Mar-23 13:55 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi> Added a little more to my code (see below). It now automatically setsthe> name of the file. It also does a better job of spacing the text for > pattern and patient x line at the top of the graph. > > I really like the way this looks now. I just need to figure out how to > loop through the data using my "key_line" (patient x line) variable. > > One of the things I''ve noticed while learning R is that things I think > will be difficult often go surprisingly well. It''s the things that Ithink> will be easy that I wind up struggling with. Right now I''m strugglingwith> figuring out how to loop through the data to produce plot11, plot 12, > plot21, and plot22. > > Embarassing. But there it is.I would split original TestData to required groups according profile_key and line to a list. After that you can go through resulting list in a cycle for (i in length of a list) { all manipulation melting and plotting } Do not forget to enclose ggplot to print(ggplot....). Regards Petr> > Can you show me how to do that? In the meantime, I keep working on itand> may figure it out on my own. > > 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 == 1 & TestData$line == 1,] > TestData > > require(reshape) > TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) > TestData$drug <- factor(TestData$drug, levels = c("Drug D", "Drug C", > "Drug B", "Drug A")) > TestData$key_line <- with(TestData,paste(profile_key, line, sep = "")) > TestData > > require(ggplot2) > > png(filename = paste("plot", unique(TestData$key_line), ".png", sep =""),> width=600, height=300) > > ggplot(TestData, aes(value, drug)) + geom_line(size = 6) + xlab("Time")+> ylab("") + theme_bw() + > opts(title = paste("Pattern = ", unique(TestData > $pattern), " \n (profile_key = ", unique(TestData$profile_key), ", line=> ", unique(TestData$line), ") \n", sep = "")) + > opts(axis.text.x = theme_blank() ) > > dev.off() > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.

Paul Miller

2012-Mar-23 14:22 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Michael and Petr, You both seem to have hit on the idea of splitting the TestData in order to do by group processing. Trouble is that ggplot2 doesn''t seem to like lists very much. When I run the code: TempData <- split(TestData, TestData$key_line) TempData for(temp in TempData){ png(filename = paste("plot", unique(TempData$key_line), ".png", sep = ""), width=600, height=300) ggplot(TempData, aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + opts(title = paste("Pattern = ", unique(TempData$pattern), " \n (profile_key = ", unique(TempData$profile_key), ", line = ", unique(TempData$line), ") \n", sep = "")) + opts(axis.text.x = theme_blank() ) dev.off() } I get the error message: "Error: ggplot2 doesn''t know how to deal with data of class list" Are there any other good ways of doing the looping? Sorry to trouble you with this. If I had more time, I''d just struggle with it for awhile and figure it out myself. I tried embedding my ggplot code into print() as Petr suggested. I didn''t think it would help but wanted to try just in case. No dice -- ggplot just doesn''t seem to like lists. Thanks, Paul --- On Fri, 3/23/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: Friday, March 23, 2012, 8:52 AM > Inline. > > On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> > wrote: > > Hi Michael, > > > > Added a little more to my code (see below). It now > automatically sets the name of the file. It also does a > better job of spacing the text for pattern and patient x > line at the top of the graph. > > > > I really like the way this looks now. I just need to > figure out how to loop through the data using my "key_line" > (patient x line) variable. > > > > One of the things I''ve noticed while learning R is that > things I think will be difficult often go surprisingly well. > It''s the things that I think will be easy that I wind up > struggling with. Right now I''m struggling with figuring out > how to loop through the data to produce plot11, plot 12, > plot21, and plot22. > > > > Embarassing. But there it is. > > > > Can you show me how to do that? In the meantime, I keep > working on it and may figure it out on my own. > > > > 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 == 1 & > TestData$line == 1,] > > TestData > > > > require(reshape) > > TestData <- melt(TestData, measure.vars > c("start_drug", "stop_drug")) > > TestData$drug <- factor(TestData$drug, levels > c("Drug D", "Drug C", "Drug B", "Drug A")) > > TestData$key_line <- > with(TestData,paste(profile_key, line, sep = "")) > > TestData > > Useful trick: if you use dput() you can send this all in a > much more > concise fashion: > > structure(list(profile_key = c(1, 1, 1, 1), line = c(1, 1, > 1, > 1), drug = structure(c(4L, 3L, 4L, 3L), .Label = c("Drug > D", > "Drug C", "Drug B", "Drug A"), class = "factor"), pattern > structure(c(4L, > 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, B), End > (C)", > " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), > Begin (C), > End (C), Begin (D), End (D)", > " Begin (A), Begin (B), End (B), End (A)"), class > "factor"), > ? ? variable = structure(c(1L, 1L, 2L, 2L), .Label > = c("start_drug", > ? ? "stop_drug"), class = "factor"), value = c(0, > 0.7143, 21, > ? ? 18), key_line = c("11", "11", "11", "11")), > .Names = c("profile_key", > "line", "drug", "pattern", "variable", "value", "key_line"), > row.names = c(NA, > -4L), class = "data.frame") > > > > > > require(ggplot2) > > > > png(filename = paste("plot", unique(TestData$key_line), > ".png", sep = ""), width=600, height=300) > > > > ggplot(TestData, aes(value, drug)) + geom_line(size > 6) + xlab("Time") + ylab("") + theme_bw() + > > ? ? ? ? ? ? ? ? opts(title = paste("Pattern > ", unique(TestData$pattern), " \n (profile_key = ", > unique(TestData$profile_key), ", line = ", > unique(TestData$line), ") \n", sep = "")) + > > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x > theme_blank() ?) > > > > dev.off() > > > > > > If you want to loop over the different values of "key_line", > I think > it''s pretty easy: > > TempData <- split(TestData, TestData$keyline) # List of > data frames > > for(temp in TempData){ # Loop over the list > > ## Do all your stuff -- just change "TestData" to "temp" so > you are > using the right data.frame > > > } > > Hope this helps, > > Michael >

Dear Petr Savicky, this helped indeed. Thank you very much. Gildas ----- Mail original -----> De: "Petr Savicky" <savicky at cs.cas.cz> > ?: r-help at r-project.org > Envoy?: Vendredi 23 Mars 2012 09:39:37 > Objet: Re: [R] Computing High Order Derivatives (Numerically) > On Fri, Mar 23, 2012 at 12:35:57AM +0100, Gildas Mazo wrote: > > Dear R users, > > > > Let f be a function over d variables x1,..,xd. I want to compute the > > k^th-order derivative with respect to x1,..,xk (k<=d). I have a by > > hand solution (see below) using an iterating code using D. However, > > I expect d to be high and f to be complicated. Then I want a vector > > x to be the input, instead of x1,..,xd. How to avoid the x1 <- x[1]; > > x2 <- x[2], etc steps in the code below? Moreover, D uses symbolic > > differentation and then eval evaluates the output to get a numerical > > result. But is there a way to compute the desired derivatives > > numerically directly (without using symbolic calculus at all)? > > Finally, what is the most efficient and fast way to get a numerical > > result for such derivatives? > > > > Thank you very much in advance, > > Gildas > > > > ### Code ### > > ### dif takes a function f, an order k, and a vector x as input. f > > must be a function of x1,..,xd with d >= k. The correspondance is > > done between xi and x[i]. The expression for f must be at the last > > row of the body function. > > dif <- function(f,k,x){ > > o <- list() > > n <- length(body(f)) > > o[[1]] <- body(f)[[n]] > > for (i in 1:k){ > > xi <- paste("x",i,sep="") > > o[[i+1]] <- D(o[[i]],name=xi) > > } > > x1 <- x[1] > > x2 <- x[2] > > x3 <- x[3] > > eval(o[[k+1]]) > > } > > > > ### Examples ### > > ## function to differentiate > > f <- function(x){ > > x1 <- x[1] > > x2 <- x[2] > > x3 <- x[3] > > 0.5*x1*x2*x3^2 > > } > > ## derivative w.r.t. x1, x2 and x3 at the point (1,2,3). > > dif(f,3,c(1,2,3)) > > > > ### My Questions ### > > ## how to avoid to write by hand xi <- x[i] ?? > > ## is there a way in R to compute such derivatives without using > > symbolic calculation but numerical compuation instead. > > Hi. > > For the first question, try the following > > dif <- function(f,k,x){ > o <- list() > n <- length(body(f)) > o[[1]] <- body(f)[[n]] > for (i in 1:k){ > xi <- paste("x",i,sep="") > o[[i+1]] <- D(o[[i]],name=xi) > assign(xi, x[i]) > } > eval(o[[k+1]]) > } > > For the second question, try the following. > > x <- c(1, 2, 3) > k <- length(x) > grid <- as.matrix(expand.grid(rep(list(c(0, 1)), times=k))) > signs <- 1 - 2*(rowSums(1 - grid) %% 2) > for (eps in 2^-(5:20)) { > xeps <- eps*grid + rep(x, each=nrow(grid)) > print(sum(signs*apply(xeps, 1, FUN=f))/eps^k) > } > > [1] 3.015625 > [1] 3.007812 > [1] 3.003906 > [1] 3.001953 > [1] 3.000977 > [1] 3.000488 > [1] 3.000244 > [1] 3.000122 > [1] 3 > [1] 3 > [1] 3 > [1] 3 > [1] 4 > [1] 0 > [1] 0 > [1] 0 > > If the above is computed in an exact arithmetic, then > with "eps" converging to zero, the result converges to > the required derivative. Since the numerical computations > are done with a rounding error, too small "eps" yields > a completely wrong result. The choice of a good "eps" > depends on the function and on "k". For a high "k", there > may even be no good "eps". See the considerations at > > http://en.wikipedia.org/wiki/Numerical_derivative > > where the choice of "eps" is discussed in the simplest > case of a univariate function. > > Hope this helps. > > Petr Savicky. > > ______________________________________________ > 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.-- Gildas Mazo PhD student MISTIS team at INRIA Grenoble, France

R. Michael Weylandt

2012-Mar-23 14:37 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

You didn''t do what I said..... Once you make the list of data.frame()s "TempData", each single element is a data.frame and that is what you need to pass to ggplot -- in the loop construct I set up, the individual data frame is called temp so you need to have ggplot(temp). As I said before, ## Do all your stuff -- just change "TestData" to "temp" so you are using the right data.frame I''ll be a little more direct this time: for(temp in TempData){ png(filename = paste("plot", unique(temp$key_line), ".png", sep = ""), width=600, height=300) p <- ggplot(temp, aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + opts(title = paste("Pattern ", unique(temp$pattern), " \n (profile_key = ", unique(temp$profile_key), ", line = ", unique(temp$line), ") \n", sep = "")) + opts(axis.text.x = theme_blank()) print(p) dev.off() } Michael On Fri, Mar 23, 2012 at 10:22 AM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hi Michael and Petr, > > You both seem to have hit on the idea of splitting the TestData in order to do by group processing. Trouble is that ggplot2 doesn''t seem to like lists very much. > > When I run the code: > > TempData <- split(TestData, TestData$key_line) > TempData > > for(temp in TempData){ > > png(filename = paste("plot", unique(TempData$key_line), ".png", sep = ""), width=600, height=300) > > ggplot(TempData, aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + > ? ? ? ? ? ? ? ? opts(title = paste("Pattern = ", unique(TempData$pattern), " \n (profile_key = ", unique(TempData$profile_key), ", line = ", unique(TempData$line), ") \n", sep = "")) + > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x = theme_blank() ?) > dev.off() > > } > > I get the error message: > > "Error: ggplot2 doesn''t know how to deal with data of class list" > > Are there any other good ways of doing the looping? Sorry to trouble you with this. If I had more time, I''d just struggle with it for awhile and figure it out myself. > > I tried embedding my ggplot code into print() as Petr suggested. I didn''t think it would help but wanted to try just in case. No dice -- ggplot just doesn''t seem to like lists. > > Thanks, > > Paul > > > > --- On Fri, 3/23/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: Friday, March 23, 2012, 8:52 AM >> Inline. >> >> On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> >> wrote: >> > Hi Michael, >> > >> > Added a little more to my code (see below). It now >> automatically sets the name of the file. It also does a >> better job of spacing the text for pattern and patient x >> line at the top of the graph. >> > >> > I really like the way this looks now. I just need to >> figure out how to loop through the data using my "key_line" >> (patient x line) variable. >> > >> > One of the things I''ve noticed while learning R is that >> things I think will be difficult often go surprisingly well. >> It''s the things that I think will be easy that I wind up >> struggling with. Right now I''m struggling with figuring out >> how to loop through the data to produce plot11, plot 12, >> plot21, and plot22. >> > >> > Embarassing. But there it is. >> > >> > Can you show me how to do that? In the meantime, I keep >> working on it and may figure it out on my own. >> > >> > 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 == 1 & >> TestData$line == 1,] >> > TestData >> > >> > require(reshape) >> > TestData <- melt(TestData, measure.vars >> c("start_drug", "stop_drug")) >> > TestData$drug <- factor(TestData$drug, levels >> c("Drug D", "Drug C", "Drug B", "Drug A")) >> > TestData$key_line <- >> with(TestData,paste(profile_key, line, sep = "")) >> > TestData >> >> Useful trick: if you use dput() you can send this all in a >> much more >> concise fashion: >> >> structure(list(profile_key = c(1, 1, 1, 1), line = c(1, 1, >> 1, >> 1), drug = structure(c(4L, 3L, 4L, 3L), .Label = c("Drug >> D", >> "Drug C", "Drug B", "Drug A"), class = "factor"), pattern >> structure(c(4L, >> 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, B), End >> (C)", >> " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), >> Begin (C), >> End (C), Begin (D), End (D)", >> " Begin (A), Begin (B), End (B), End (A)"), class >> "factor"), >> ? ? variable = structure(c(1L, 1L, 2L, 2L), .Label >> = c("start_drug", >> ? ? "stop_drug"), class = "factor"), value = c(0, >> 0.7143, 21, >> ? ? 18), key_line = c("11", "11", "11", "11")), >> .Names = c("profile_key", >> "line", "drug", "pattern", "variable", "value", "key_line"), >> row.names = c(NA, >> -4L), class = "data.frame") >> >> >> > >> > require(ggplot2) >> > >> > png(filename = paste("plot", unique(TestData$key_line), >> ".png", sep = ""), width=600, height=300) >> > >> > ggplot(TestData, aes(value, drug)) + geom_line(size >> 6) + xlab("Time") + ylab("") + theme_bw() + >> > ? ? ? ? ? ? ? ? opts(title = paste("Pattern >> ", unique(TestData$pattern), " \n (profile_key = ", >> unique(TestData$profile_key), ", line = ", >> unique(TestData$line), ") \n", sep = "")) + >> > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x >> theme_blank() ?) >> > >> > dev.off() >> > >> > >> >> If you want to loop over the different values of "key_line", >> I think >> it''s pretty easy: >> >> TempData <- split(TestData, TestData$keyline) # List of >> data frames >> >> for(temp in TempData){ # Loop over the list >> >> ## Do all your stuff -- just change "TestData" to "temp" so >> you are >> using the right data.frame >> >> >> } >> >> Hope this helps, >> >> Michael >>

Petr PIKAL

2012-Mar-23 14:37 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi> > Hi Michael and Petr, > > You both seem to have hit on the idea of splitting the TestData in order> to do by group processing. Trouble is that ggplot2 doesn''t seem to like > lists very much. > > When I run the code: > > TempData <- split(TestData, TestData$key_line) > TempData > > for(temp in TempData){ > > png(filename = paste("plot", unique(TempData$key_line), ".png", sep =""),> width=600, height=300) >You must use print and select from your TempData list only one value by subsetting it by temp. You need to use propper subsetting also in creating png plots. In each cycle you will select values by TempData[temp]. Regards Petr print( ggplot(TempData[temp], aes(value, drug)) + geom_line(size = 6) + xlab("Time") + ylab("") + theme_bw() + opts(title = paste("Pattern = ", unique(TempData$pattern), " \n (profile_key = ", unique(TempData$profile_key), ", line = ", unique(TempData$line), ") \n", sep = "")) + opts(axis.text.x = theme_blank() ) )> ggplot(TempData, aes(value, drug)) + geom_line(size = 6) + xlab("Time")+> ylab("") + theme_bw() + > opts(title = paste("Pattern = ", unique(TempData > $pattern), " \n (profile_key = ", unique(TempData$profile_key), ", line=> ", unique(TempData$line), ") \n", sep = "")) + > opts(axis.text.x = theme_blank() )> dev.off() > > } > > I get the error message: > > "Error: ggplot2 doesn''t know how to deal with data of class list" > > Are there any other good ways of doing the looping? Sorry to trouble you> with this. If I had more time, I''d just struggle with it for awhile and > figure it out myself. > > I tried embedding my ggplot code into print() as Petr suggested. Ididn''t> think it would help but wanted to try just in case. No dice -- ggplotjust> doesn''t seem to like lists. > > Thanks, > > Paul > > > > --- On Fri, 3/23/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 (orsome> other means) -- Help!!! > > To: "Paul Miller" <pjmiller_57 at yahoo.com> > > Cc: r-help at r-project.org > > Received: Friday, March 23, 2012, 8:52 AM > > Inline. > > > > On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> > > wrote: > > > Hi Michael, > > > > > > Added a little more to my code (see below). It now > > automatically sets the name of the file. It also does a > > better job of spacing the text for pattern and patient x > > line at the top of the graph. > > > > > > I really like the way this looks now. I just need to > > figure out how to loop through the data using my "key_line" > > (patient x line) variable. > > > > > > One of the things I''ve noticed while learning R is that > > things I think will be difficult often go surprisingly well. > > It''s the things that I think will be easy that I wind up > > struggling with. Right now I''m struggling with figuring out > > how to loop through the data to produce plot11, plot 12, > > plot21, and plot22. > > > > > > Embarassing. But there it is. > > > > > > Can you show me how to do that? In the meantime, I keep > > working on it and may figure it out on my own. > > > > > > 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 == 1 & > > TestData$line == 1,] > > > TestData > > > > > > require(reshape) > > > TestData <- melt(TestData, measure.vars > > c("start_drug", "stop_drug")) > > > TestData$drug <- factor(TestData$drug, levels > > c("Drug D", "Drug C", "Drug B", "Drug A")) > > > TestData$key_line <- > > with(TestData,paste(profile_key, line, sep = "")) > > > TestData > > > > Useful trick: if you use dput() you can send this all in a > > much more > > concise fashion: > > > > structure(list(profile_key = c(1, 1, 1, 1), line = c(1, 1, > > 1, > > 1), drug = structure(c(4L, 3L, 4L, 3L), .Label = c("Drug > > D", > > "Drug C", "Drug B", "Drug A"), class = "factor"), pattern > > structure(c(4L, > > 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, B), End > > (C)", > > " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), > > Begin (C), > > End (C), Begin (D), End (D)", > > " Begin (A), Begin (B), End (B), End (A)"), class > > "factor"), > > variable = structure(c(1L, 1L, 2L, 2L), .Label > > = c("start_drug", > > "stop_drug"), class = "factor"), value = c(0, > > 0.7143, 21, > > 18), key_line = c("11", "11", "11", "11")), > > .Names = c("profile_key", > > "line", "drug", "pattern", "variable", "value", "key_line"), > > row.names = c(NA, > > -4L), class = "data.frame") > > > > > > > > > > require(ggplot2) > > > > > > png(filename = paste("plot", unique(TestData$key_line), > > ".png", sep = ""), width=600, height=300) > > > > > > ggplot(TestData, aes(value, drug)) + geom_line(size > > 6) + xlab("Time") + ylab("") + theme_bw() + > > > opts(title = paste("Pattern > > ", unique(TestData$pattern), " \n (profile_key = ", > > unique(TestData$profile_key), ", line = ", > > unique(TestData$line), ") \n", sep = "")) + > > > opts(axis.text.x > > theme_blank() ) > > > > > > dev.off() > > > > > > > > > > If you want to loop over the different values of "key_line", > > I think > > it''s pretty easy: > > > > TempData <- split(TestData, TestData$keyline) # List of > > data frames > > > > for(temp in TempData){ # Loop over the list > > > > ## Do all your stuff -- just change "TestData" to "temp" so > > you are > > using the right data.frame > > > > > > } > > > > Hope this helps, > > > > Michael > >

Paul Miller

2012-Mar-23 18:15 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hi Michael and Petr, Apologize for my failure to grasp what you were saying. My code is up and running now. Noticed what might be a shortcoming of my ggplot code. I have some instances where a drug starts and stops and then starts and stops again. It looks like my graphs show just a single unbroken line segment though. I ordered Hadley Wickham''s ggplot2 book earlier today. So hopefully I''ll be able to figure that out myself once the book arrives. Thank you Michael, Petr, and Bert for your help with this. Thanks especially to Michael for patiently answering all my questions over the last day or so. Paul --- On Fri, 3/23/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: "Petr PIKAL" <petr.pikal at precheza.cz>, r-help at r-project.org > Received: Friday, March 23, 2012, 9:37 AM > You didn''t do what I said..... > > Once you make the list of data.frame()s "TempData", each > single > element is a data.frame and that is what you need to pass to > ggplot -- > in the loop construct I set up, the individual data frame is > called > temp so you need to have ggplot(temp). > > As I said before, > > ## Do all your stuff -- just change "TestData" to "temp" so > you are > using the right data.frame > > I''ll be a little more direct this time: > > for(temp in TempData){ > > png(filename = paste("plot", unique(temp$key_line), ".png", > sep = ""), > width=600, height=300) > p <- ggplot(temp, aes(value, drug)) + geom_line(size = 6) > + > xlab("Time") + ylab("") + theme_bw() +? opts(title > paste("Pattern > ", unique(temp$pattern), " \n (profile_key = ", > unique(temp$profile_key), ", line = ", unique(temp$line), ") > \n", sep > = "")) + opts(axis.text.x = theme_blank()) > print(p) > dev.off() > } > > Michael > > > On Fri, Mar 23, 2012 at 10:22 AM, Paul Miller <pjmiller_57 at yahoo.com> > wrote: > > Hi Michael and Petr, > > > > You both seem to have hit on the idea of splitting the > TestData in order to do by group processing. Trouble is that > ggplot2 doesn''t seem to like lists very much. > > > > When I run the code: > > > > TempData <- split(TestData, TestData$key_line) > > TempData > > > > for(temp in TempData){ > > > > png(filename = paste("plot", unique(TempData$key_line), > ".png", sep = ""), width=600, height=300) > > > > ggplot(TempData, aes(value, drug)) + geom_line(size > 6) + xlab("Time") + ylab("") + theme_bw() + > > ? ? ? ? ? ? ? ? opts(title = paste("Pattern > ", unique(TempData$pattern), " \n (profile_key = ", > unique(TempData$profile_key), ", line = ", > unique(TempData$line), ") \n", sep = "")) + > > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x > theme_blank() ?) > > dev.off() > > > > } > > > > I get the error message: > > > > "Error: ggplot2 doesn''t know how to deal with data of > class list" > > > > Are there any other good ways of doing the looping? > Sorry to trouble you with this. If I had more time, I''d just > struggle with it for awhile and figure it out myself. > > > > I tried embedding my ggplot code into print() as Petr > suggested. I didn''t think it would help but wanted to try > just in case. No dice -- ggplot just doesn''t seem to like > lists. > > > > Thanks, > > > > Paul > > > > > > > > --- On Fri, 3/23/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: Friday, March 23, 2012, 8:52 AM > >> Inline. > >> > >> On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> > >> wrote: > >> > Hi Michael, > >> > > >> > Added a little more to my code (see below). It > now > >> automatically sets the name of the file. It also > does a > >> better job of spacing the text for pattern and > patient x > >> line at the top of the graph. > >> > > >> > I really like the way this looks now. I just > need to > >> figure out how to loop through the data using my > "key_line" > >> (patient x line) variable. > >> > > >> > One of the things I''ve noticed while learning > R is that > >> things I think will be difficult often go > surprisingly well. > >> It''s the things that I think will be easy that I > wind up > >> struggling with. Right now I''m struggling with > figuring out > >> how to loop through the data to produce plot11, > plot 12, > >> plot21, and plot22. > >> > > >> > Embarassing. But there it is. > >> > > >> > Can you show me how to do that? In the > meantime, I keep > >> working on it and may figure it out on my own. > >> > > >> > 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 > == 1 & > >> TestData$line == 1,] > >> > TestData > >> > > >> > require(reshape) > >> > TestData <- melt(TestData, measure.vars > >> c("start_drug", "stop_drug")) > >> > TestData$drug <- factor(TestData$drug, > levels > >> c("Drug D", "Drug C", "Drug B", "Drug A")) > >> > TestData$key_line <- > >> with(TestData,paste(profile_key, line, sep = "")) > >> > TestData > >> > >> Useful trick: if you use dput() you can send this > all in a > >> much more > >> concise fashion: > >> > >> structure(list(profile_key = c(1, 1, 1, 1), line > c(1, 1, > >> 1, > >> 1), drug = structure(c(4L, 3L, 4L, 3L), .Label > c("Drug > >> D", > >> "Drug C", "Drug B", "Drug A"), class = "factor"), > pattern > >> structure(c(4L, > >> 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, > B), End > >> (C)", > >> " Begin (A, B), End (A, B)", " Begin (A, B), End > (A, B), > >> Begin (C), > >> End (C), Begin (D), End (D)", > >> " Begin (A), Begin (B), End (B), End (A)"), class > > >> "factor"), > >> ? ? variable = structure(c(1L, 1L, 2L, 2L), > .Label > >> = c("start_drug", > >> ? ? "stop_drug"), class = "factor"), value > c(0, > >> 0.7143, 21, > >> ? ? 18), key_line = c("11", "11", "11", "11")), > >> .Names = c("profile_key", > >> "line", "drug", "pattern", "variable", "value", > "key_line"), > >> row.names = c(NA, > >> -4L), class = "data.frame") > >> > >> > >> > > >> > require(ggplot2) > >> > > >> > png(filename = paste("plot", > unique(TestData$key_line), > >> ".png", sep = ""), width=600, height=300) > >> > > >> > ggplot(TestData, aes(value, drug)) + > geom_line(size > >> 6) + xlab("Time") + ylab("") + theme_bw() + > >> > ? ? ? ? ? ? ? ? opts(title > paste("Pattern > >> ", unique(TestData$pattern), " \n (profile_key > ", > >> unique(TestData$profile_key), ", line = ", > >> unique(TestData$line), ") \n", sep = "")) + > >> > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x > > >> theme_blank() ?) > >> > > >> > dev.off() > >> > > >> > > >> > >> If you want to loop over the different values of > "key_line", > >> I think > >> it''s pretty easy: > >> > >> TempData <- split(TestData, TestData$keyline) # > List of > >> data frames > >> > >> for(temp in TempData){ # Loop over the list > >> > >> ## Do all your stuff -- just change "TestData" to > "temp" so > >> you are > >> using the right data.frame > >> > >> > >> } > >> > >> Hope this helps, > >> > >> Michael > >> >

R. Michael Weylandt

2012-Mar-23 18:20 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

The ggplot book is quite good, but be forewarned, there have been some structural changes of late and some of the book code won''t work quite as is anymore: this document describes them (the changes, not the old code) in some detail if you start hitting those sorts of things: https://github.com/downloads/hadley/ggplot2/guide-col.pdf Best of luck, Michael On Fri, Mar 23, 2012 at 2:15 PM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hi Michael and Petr, > > Apologize for my failure to grasp what you were saying. My code is up and running now. > > Noticed what might be a shortcoming of my ggplot code. I have some instances where a drug starts and stops and then starts and stops again. It looks like my graphs show just a single unbroken line segment though. > I ordered Hadley Wickham''s ggplot2 book earlier today. So hopefully I''ll be able to figure that out myself once the book arrives. > > Thank you Michael, Petr, and Bert for your help with this. Thanks especially to Michael for patiently answering all my questions over the last day or so. > > Paul > > > > --- On Fri, 3/23/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: "Petr PIKAL" <petr.pikal at precheza.cz>, r-help at r-project.org >> Received: Friday, March 23, 2012, 9:37 AM >> You didn''t do what I said..... >> >> Once you make the list of data.frame()s "TempData", each >> single >> element is a data.frame and that is what you need to pass to >> ggplot -- >> in the loop construct I set up, the individual data frame is >> called >> temp so you need to have ggplot(temp). >> >> As I said before, >> >> ?## Do all your stuff -- just change "TestData" to "temp" so >> you are >> using the right data.frame >> >> I''ll be a little more direct this time: >> >> for(temp in TempData){ >> >> png(filename = paste("plot", unique(temp$key_line), ".png", >> sep = ""), >> width=600, height=300) >> p <- ggplot(temp, aes(value, drug)) + geom_line(size = 6) >> + >> xlab("Time") + ylab("") + theme_bw() +? opts(title >> paste("Pattern >> ", unique(temp$pattern), " \n (profile_key = ", >> unique(temp$profile_key), ", line = ", unique(temp$line), ") >> \n", sep >> = "")) + opts(axis.text.x = theme_blank()) >> print(p) >> dev.off() >> } >> >> Michael >> >> >> On Fri, Mar 23, 2012 at 10:22 AM, Paul Miller <pjmiller_57 at yahoo.com> >> wrote: >> > Hi Michael and Petr, >> > >> > You both seem to have hit on the idea of splitting the >> TestData in order to do by group processing. Trouble is that >> ggplot2 doesn''t seem to like lists very much. >> > >> > When I run the code: >> > >> > TempData <- split(TestData, TestData$key_line) >> > TempData >> > >> > for(temp in TempData){ >> > >> > png(filename = paste("plot", unique(TempData$key_line), >> ".png", sep = ""), width=600, height=300) >> > >> > ggplot(TempData, aes(value, drug)) + geom_line(size >> 6) + xlab("Time") + ylab("") + theme_bw() + >> > ? ? ? ? ? ? ? ? opts(title = paste("Pattern >> ", unique(TempData$pattern), " \n (profile_key = ", >> unique(TempData$profile_key), ", line = ", >> unique(TempData$line), ") \n", sep = "")) + >> > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x >> theme_blank() ?) >> > dev.off() >> > >> > } >> > >> > I get the error message: >> > >> > "Error: ggplot2 doesn''t know how to deal with data of >> class list" >> > >> > Are there any other good ways of doing the looping? >> Sorry to trouble you with this. If I had more time, I''d just >> struggle with it for awhile and figure it out myself. >> > >> > I tried embedding my ggplot code into print() as Petr >> suggested. I didn''t think it would help but wanted to try >> just in case. No dice -- ggplot just doesn''t seem to like >> lists. >> > >> > Thanks, >> > >> > Paul >> > >> > >> > >> > --- On Fri, 3/23/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: Friday, March 23, 2012, 8:52 AM >> >> Inline. >> >> >> >> On Fri, Mar 23, 2012 at 9:40 AM, Paul Miller <pjmiller_57 at yahoo.com> >> >> wrote: >> >> > Hi Michael, >> >> > >> >> > Added a little more to my code (see below). It >> now >> >> automatically sets the name of the file. It also >> does a >> >> better job of spacing the text for pattern and >> patient x >> >> line at the top of the graph. >> >> > >> >> > I really like the way this looks now. I just >> need to >> >> figure out how to loop through the data using my >> "key_line" >> >> (patient x line) variable. >> >> > >> >> > One of the things I''ve noticed while learning >> R is that >> >> things I think will be difficult often go >> surprisingly well. >> >> It''s the things that I think will be easy that I >> wind up >> >> struggling with. Right now I''m struggling with >> figuring out >> >> how to loop through the data to produce plot11, >> plot 12, >> >> plot21, and plot22. >> >> > >> >> > Embarassing. But there it is. >> >> > >> >> > Can you show me how to do that? In the >> meantime, I keep >> >> working on it and may figure it out on my own. >> >> > >> >> > 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 >> == 1 & >> >> TestData$line == 1,] >> >> > TestData >> >> > >> >> > require(reshape) >> >> > TestData <- melt(TestData, measure.vars >> >> c("start_drug", "stop_drug")) >> >> > TestData$drug <- factor(TestData$drug, >> levels >> >> c("Drug D", "Drug C", "Drug B", "Drug A")) >> >> > TestData$key_line <- >> >> with(TestData,paste(profile_key, line, sep = "")) >> >> > TestData >> >> >> >> Useful trick: if you use dput() you can send this >> all in a >> >> much more >> >> concise fashion: >> >> >> >> structure(list(profile_key = c(1, 1, 1, 1), line >> c(1, 1, >> >> 1, >> >> 1), drug = structure(c(4L, 3L, 4L, 3L), .Label >> c("Drug >> >> D", >> >> "Drug C", "Drug B", "Drug A"), class = "factor"), >> pattern >> >> structure(c(4L, >> >> 4L, 4L, 4L), .Label = c(" Begin (A, B, C), End (A, >> B), End >> >> (C)", >> >> " Begin (A, B), End (A, B)", " Begin (A, B), End >> (A, B), >> >> Begin (C), >> >> End (C), Begin (D), End (D)", >> >> " Begin (A), Begin (B), End (B), End (A)"), class >> >> >> "factor"), >> >> ? ? variable = structure(c(1L, 1L, 2L, 2L), >> .Label >> >> = c("start_drug", >> >> ? ? "stop_drug"), class = "factor"), value >> c(0, >> >> 0.7143, 21, >> >> ? ? 18), key_line = c("11", "11", "11", "11")), >> >> .Names = c("profile_key", >> >> "line", "drug", "pattern", "variable", "value", >> "key_line"), >> >> row.names = c(NA, >> >> -4L), class = "data.frame") >> >> >> >> >> >> > >> >> > require(ggplot2) >> >> > >> >> > png(filename = paste("plot", >> unique(TestData$key_line), >> >> ".png", sep = ""), width=600, height=300) >> >> > >> >> > ggplot(TestData, aes(value, drug)) + >> geom_line(size >> >> 6) + xlab("Time") + ylab("") + theme_bw() + >> >> > ? ? ? ? ? ? ? ? opts(title >> paste("Pattern >> >> ", unique(TestData$pattern), " \n (profile_key >> ", >> >> unique(TestData$profile_key), ", line = ", >> >> unique(TestData$line), ") \n", sep = "")) + >> >> > ? ? ? ? ? ? ? ? ? ? opts(axis.text.x >> >> >> theme_blank() ?) >> >> > >> >> > dev.off() >> >> > >> >> > >> >> >> >> If you want to loop over the different values of >> "key_line", >> >> I think >> >> it''s pretty easy: >> >> >> >> TempData <- split(TestData, TestData$keyline) # >> List of >> >> data frames >> >> >> >> for(temp in TempData){ # Loop over the list >> >> >> >> ## Do all your stuff -- just change "TestData" to >> "temp" so >> >> you are >> >> using the right data.frame >> >> >> >> >> >> } >> >> >> >> Hope this helps, >> >> >> >> Michael >> >> >>

David Winsemius

2012-Mar-23 18:23 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

On Mar 23, 2012, at 2:15 PM, Paul Miller wrote:> Hi Michael and Petr, > > Apologize for my failure to grasp what you were saying. My code is > up and running now. > > Noticed what might be a shortcoming of my ggplot code. I have some > instances where a drug starts and stops and then starts and stops > again. It looks like my graphs show just a single unbroken line > segment though.Put in NA entries at times you do not want plotted. Not sure exactly how that gets handled in ggplot but since plotting nothing was the "usual" behavior in base and lattice graphics, I would think that would have gotten carried over.> I ordered Hadley Wickham''s ggplot2 book earlier today. So hopefully > I''ll be able to figure that out myself once the book arrives. > > Thank you Michael, Petr, and Bert for your help with this. Thanks > especially to Michael for patiently answering all my questions over > the last day or so. > > PaulDavid Winsemius, MD West Hartford, CT

Paul Miller

2012-Mar-27 20:03 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hello Dr. Winsemius, Not sure how or if the use of NAs you describe applies to my case. I''ll go back to this again when the ggplot2 book arrives. It may be that this will provide a helpful insight then. Thanks, Paul --- On Fri, 3/23/12, David Winsemius <dwinsemius at comcast.net> wrote:> From: David Winsemius <dwinsemius at comcast.net> > Subject: Re: [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!! > To: "Paul Miller" <pjmiller_57 at yahoo.com> > Cc: "R. Michael Weylandt" <michael.weylandt at gmail.com>, "Petr PIKAL" <petr.pikal at precheza.cz>, "Bert Gunter" <gunter.berton at gene.com>, r-help at r-project.org > Received: Friday, March 23, 2012, 1:23 PM > > On Mar 23, 2012, at 2:15 PM, Paul Miller wrote: > > > Hi Michael and Petr, > > > > Apologize for my failure to grasp what you were saying. > My code is up and running now. > > > > Noticed what might be a shortcoming of my ggplot code. > I have some instances where a drug starts and stops and then > starts and stops again. It looks like my graphs show just a > single unbroken line segment though. > > Put in NA entries at times you do not want plotted. Not sure > exactly how that gets handled in ggplot but since plotting > nothing was the "usual" behavior in base and lattice > graphics, I would think that would have gotten carried > over. > > > > I ordered Hadley Wickham''s ggplot2 book earlier today. > So hopefully I''ll be able to figure that out myself once the > book arrives. > > > > Thank you Michael, Petr, and Bert for your help with > this. Thanks especially to Michael for patiently answering > all my questions over the last day or so. > > > > Paul > > > David Winsemius, MD > West Hartford, CT > >

Paul Miller

2012-Mar-28 17:19 UTC

### [R] Plotting patient drug timelines using ggplot2 (or some other means) -- Help!!!

Hello All, Figured out how to get the gaps in the bars when a drug stops and the starts again (see below). Made the graph overlaid/superimposed/stacked and got the desired result. Not sure this is how an expert would do it. But it''s simple and it works. Paul setwd("N:/Regimen Coding/0906/Plots Test") getwd() connection <- textConnection(" 1/1/1/Drug A/ Begin (A), Begin (B), End (B), End (A)/0.0000/21.000 1/1/1/Drug B/ Begin (A), Begin (B), End (B), End (A)/0.7143/18.000 1/2/1/Drug A/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 1/2/1/Drug B/ Begin (A, B, C), End (A, B), End (C)/0.0000/20.000 1/2/1/Drug C/ Begin (A, B, C), End (A, B), End (C)/0.0000/36.000 2/1/1/Drug A/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/0.0000/7.429 2/1/1/Drug B/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/0.0000/7.429 2/1/1/Drug C/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/14.5714/21.857 2/1/1/Drug D/ Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)/25.4286/231.286 2/2/1/Drug A/ Begin (A, B), End (A, B)/0.0000/35.286 2/2/1/Drug B/ Begin (A, B), End (A, B)/0.0000/35.286 3/1/1/Drug B/Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)/0/17.0000 3/1/1/Drug A/Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)/0/17.0000 3/1/1/Drug C/Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)/0/17.0000 3/1/1/Drug D/Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)/20/32.8571 3/1/2/Drug C/Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)/18/32.8571 ") TestData <- data.frame(scan(connection, list(profile_key=0, line=0, instance=0, drug="", pattern="", start_drug=0, stop_drug=0), sep="/")) closeAllConnections() TestData require(reshape) TestData <- melt(TestData, measure.vars = c("start_drug", "stop_drug")) TestData$drug <- factor(TestData$drug, levels = c("Drug D", "Drug C", "Drug B", "Drug A")) TestData$key_line <- with(TestData,paste(profile_key, line, sep = "")) TestData require(ggplot2) temp <- TestData TempData <- split(TestData, TestData$key_line) for(temp in TempData){ png(filename = paste("plot", unique(temp$key_line), ".png", sep = ""), width=600, height=300) p <- ggplot(temp, aes(value, drug, fill = factor(instance))) + geom_line(size = 6) + xlab("\n Time (Weeks)") + ylab("") + theme_bw() + opts(title = paste("Pattern = ", unique(temp$pattern), " \n (profile_key = ", unique(temp$profile_key), ", line = ", unique(temp$line), ") \n", sep = "")) + opts(legend.position="none") print(p) dev.off() }