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

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 23 14:55:06 CET 2012


Hi


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

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 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-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list