[R] Adding text for written comments to bottom of graphs

Paul Miller pjmiller_57 at yahoo.com
Fri Mar 30 17:08:35 CEST 2012


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



More information about the R-help mailing list