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

Paul Miller pjmiller_57 at yahoo.com
Fri Mar 23 15:22:01 CET 2012


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
>



More information about the R-help mailing list