Hi, I would do the following,
library(ggplot2) require(reshape) TestData <- structure(list(profile_key = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), line = c(1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1), instance = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2), drug = structure(c(1L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 2L, 1L, 3L, 4L, 3L), .Label = c("Drug A", "Drug B", "Drug C", "Drug D"), class = "factor"), pattern = structure(c(1L, 1L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 5L, 5L, 5L, 5L, 5L), .Label = c(" Begin (A), Begin (B), End (B), End (A)", " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), Begin (C), End (C), Begin (D), End (D)", " Begin (A, B, C), End (A, B), End (C)", "Begin (A, B, C), End (A, B, C), Begin (C), Begin (D), End (C, D)" ), class = "factor"), start_drug = c(0, 0.7143, 0, 0, 0, 0, 0, 14.5714, 25.4286, 0, 0, 0, 0, 0, 20, 18), stop_drug = c(21, 18, 20, 20, 36, 7.429, 7.429, 21.857, 231.286, 35.286, 35.286, 17, 17, 17, 32.8571, 32.8571)), .Names = c("profile_key", "line", "instance", "drug", "pattern", "start_drug", "stop_drug"), row.names = c(NA, -16L), class = "data.frame") 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 = "")) str(TestData) ## wrapper to create one title make_title <- function(d){ with(d, paste("Pattern = ", unique(pattern), " \n (profile_key = ", unique(profile_key), ", line = ", unique(line), ") \n", sep = "")) } ## create one plot given a sub-data.frame make_plot <- function(d){ ggplot(TestData, aes(value, drug, fill = factor(instance))) + geom_line(size = 6) + xlab("\n Time (Weeks)") + ylab("") + theme_bw() + opts(title=make_title(d), legend.position="none") } library(plyr) ## apply the function to each subset and store in a list plots <- dlply(TestData, "key_line", make_plot) library(gridExtra) annotation <- paste( "Regimen 1: __________________________________________________________", "\n", "\n", "Regimen 2: __________________________________________________________", "\n", "\n", "Regimen 3: __________________________________________________________", "\n", "\n", "Regimen 4: __________________________________________________________", "\n", "\n", "Regimen 5: __________________________________________________________", "\n", "\n", "Rationale: __________________________________________________________", "\n", "\n", " __________________________________________________________", "\n", "\n", " __________________________________________________________", "\n", "\n", " __________________________________________________________", "\n", "\n", " __________________________________________________________", "\n", "\n", sep="" ) ## add annotation at the bottom ("subtitle") make_annotation <- function(p, sub=textGrob(annotation, x=1, hjust=1.1)){ arrangeGrob(p, sub=sub) } ## apply annotations plots_annotated <- llply(plots, make_annotation) ## print to multi-page pdf pdf("multiple_pages.pdf", height=12, width=10) print(plots_annotated) dev.off() HTH, baptiste On 31 March 2012 04:08, Paul Miller <pjmiller...@yahoo.com> wrote: > Hello All, > > Recently developed the code below for graphing patterns of chemotherapy > administration. As someone just starting to use R in their work, I managed to > figure out some parts of the code but needed help with others. > > setwd("N:/Regimen Coding/0906/Plots Test") > getwd() > > TestData <- structure(list(profile_key = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, > 2, 3, 3, 3, 3, 3), line = c(1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, > 1, 1, 1, 1, 1), instance = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 1, 1, 1, 1, 2), drug = structure(c(1L, 2L, 1L, 2L, 3L, 1L, 2L, > 3L, 4L, 1L, 2L, 2L, 1L, 3L, 4L, 3L), .Label = c("Drug A", "Drug B", > "Drug C", "Drug D"), class = "factor"), pattern = structure(c(1L, > 1L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 5L, 5L, 5L, 5L, 5L), .Label = c(" > Begin (A), Begin (B), End (B), End (A)", > " Begin (A, B), End (A, B)", " Begin (A, B), End (A, B), Begin (C), End (C), > Begin (D), End (D)", > " Begin (A, B, C), End (A, B), End (C)", "Begin (A, B, C), End (A, B, C), > Begin (C), Begin (D), End (C, D)" > ), class = "factor"), start_drug = c(0, 0.7143, 0, 0, 0, 0, 0, > 14.5714, 25.4286, 0, 0, 0, 0, 0, 20, 18), stop_drug = c(21, 18, > 20, 20, 36, 7.429, 7.429, 21.857, 231.286, 35.286, 35.286, 17, > 17, 17, 32.8571, 32.8571)), .Names = c("profile_key", "line", > "instance", "drug", "pattern", "start_drug", "stop_drug"), row.names = c(NA, > -16L), class = "data.frame") > > 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() > } > > Would like to add lines for written comments to the bottom of each graph. > These would look something like: > > cat( > "Regimen 1: __________________________________________________________", > "\n", "\n", > "Regimen 2: __________________________________________________________", > "\n", "\n", > "Regimen 3: __________________________________________________________", > "\n", "\n", > "Regimen 4: __________________________________________________________", > "\n", "\n", > "Regimen 5: __________________________________________________________", > "\n", "\n", > "Rationale: __________________________________________________________", > "\n", "\n", > " __________________________________________________________", > "\n", "\n", > " __________________________________________________________", > "\n", "\n", > " __________________________________________________________", > "\n", "\n", > " __________________________________________________________", > "\n", "\n", > sep="" > ) > > Not sure exactly how one ought to approach this in R. Can it be done in one > step by adding to my ggplot2 code? Would it be better to do it using some > sort of post processing? Is it possible to add the text by placing what would > be the equivalent of a SAS put statement before the "dev.off()"? > > Thanks, > > Paul > > ______________________________________________ > R-help@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-help@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.