Hi:

On Fri, Oct 15, 2010 at 4:29 AM, Anh Nguyen <eatabanana@gmail.com> wrote:

> Thank you for the very helpful tips. Just one last question:
> - In the lattice method, how can I plot TIME vs OBSconcentration and TIME
> vs PREDconcentration in one graph (per individual)? You said "in lattice
> you would replace 'smooth' by 'l' in the type = argument of xyplot()" that
> just means now the OBSconc is replaced by a line instead of points and
> PREDconc is not plotted, right?
>

My mistake. That requires a little trickery. Try using both observed and
predicted as responses in the xyplot, as in

xyplot(conc + pred ~ Time | ID, ..., type = c('p', 'l'), distribute.type =
TRUE, ...)

where the ... refers to the other arguments in the xyplot() call.

If that works (and that *is* an if since it's untested), then conc should be
plotted as points and pred as lines on the same (x, y) scale. See section
5.3 of the Lattice book by Deepayan Sarkar; the code for the book example is
on its web site:

http://lmdvr.r-forge.r-project.org/figures/figures.html

The figure in question is Figure 5.12, but it uses data from several
previous figures, so you might just want to copy and paste everything from
just before Figure 5.10 up to it - the data frame in use is SeatacWeather.

--> I tried to superimpose 2 lattices but that won't work.
>

No, it won't. You might be able to get away with that in base graphics (add
= TRUE or lines/points), but not in Lattice. Actually, ggplot2 is better
positioned to handle that case 'easily'.


> I can keep the x-scale the same but not the y-scale. Ex: max pred conc = 60
> and max obs conc = 80 and thus for this case, there would be 2 different
> y-scales superimposed on one another.
>

You may need to plot concentration first if it has the higher y limit. No
guarantee that will work, but it's worth a shot. If it fails, you can set
the y-limits per panel manually using scales, something like

   scales = list(y = list(limits = list(c(0, 60), c(0, 80), c(0, 60), c(0,
60))))

in panel order, but again, that's untested and I'm not certain it would
work. Better solutions/corrections welcome.

- the ggplot2 method works, just the weird 5,6,7,8,9,10 mg legends (there
> are only 5,7,10 mg doses) but it's nothing that photoshop can't take care
> of.
>

You must have dose as numeric in your input data frame. If you convert it to
factor (e.g, with a new variable) and use the factor version of dose in
ggplot2, it should work.

>
> This is very cool, I think I understand it a lot better now. It was a lot
> easier than what I was doing before, that's for sure. Thanks!
>

For complex requests like these, it *really* helps to have a template data
frame to work with. It took me about a half hour to rearrange the Theoph
data frame from nlme into shape so that I could get a plot that remotely
resembled what you were looking for.  Alas, I don't have the time today to
do the same for this request.

HTH,
Dennis

>
> On Fri, Oct 15, 2010 at 2:32 AM, Dennis Murphy <djmuser@gmail.com> wrote:
>
>> Hi:
>>
>> To get the plots precisely as you have given them in your png file, you're
>> most likely going to have to use base graphics, especially if you want a
>> separate legend in each panel. Packages ggplot2 and lattice have more
>> structured ways of constructing such graphs, so you give up a bit of freedom
>> in the details of plot construction to get nicer default configurations.
>>
>> Perhaps you've written a panel function in S-PLUS (?) to produce each
>> graph in your png file - if so, you could simply add a couple of lines to
>> determine the max y-value and from that the limits of the y scale. It
>> shouldn't be at all difficult to make such modifications. Since R base
>> graphics is very similar to base graphics in S-PLUS, you could possibly get
>> away with popping your S-PLUS code directly into R and see how far that
>> takes you.
>>
>> Lattice is based on Trellis graphics, but the syntax in lattice has
>> changed a fair bit vis a vis Trellis as the package has developed. ggplot2
>> is a more recent graphics system in R predicated on the grammar of graphics
>> exposited by Leland Wilkinson.
>>
>> For my example, I've modified the Theophylline data set in package nlme,
>> described in Pinheiro & Bates (2000), Mixed Effects Models in S and S-PLUS,
>> Springer. The original data set has eleven unique doses - I combined them
>> into three intervals and converted them to factor with cut(). I also created
>> four groups of Subjects and put them into a variable ID. The output data
>> frame is called theo. I didn't fit the nlme models to the subjects -
>> instead, I was lazy and used loess smoothing instead. The code to generate
>> the data frame is given below; this is what we mean by a 'reproducible
>> example', something that can be copied and pasted into an R session.
>>
>> # Use modified version of Theophylline data in package nlme
>>
>> library(nlme)
>> theo <- Theoph
>> theo <- subset(theo, Dose > 3.5)
>> theo$dose <- cut(theo$Dose, breaks = c(3, 4.5, 5, 6), labels = c('4.25',
>> '4.8', '5.5'))
>> theo <- as.data.frame(theo)
>> theo$ID <- with(theo, ifelse(Subject %in% c(6, 7, 12), 1,
>>                       ifelse(Subject %in% c(2, 8, 10), 2,
>>                       ifelse(Subject %in% c(4, 11, 5), 3, 4) )))
>> # ID is used for faceting, dose for color and shape
>>
>> # lattice version:
>>
>> xyplot(conc ~ Time | factor(ID), data = theo, col.line = 1:3,
>>           pch = 1:3, col = 1:3, groups = dose, type = c('p', 'smooth'),
>>           scales = list(y = list(relation = 'free')),
>>           auto.key = list(corner = c(0.93, 0.4), lines = TRUE, points =
>> TRUE,
>>                           text = levels(theo$dose)) )
>>
>> # ggplot2 version:
>> # scales = 'free_y' allows independent y scales per panel
>> g <- ggplot(theo, aes(x = Time, y = conc, shape = dose, colour = dose,
>>                        group = Subject))
>> g + geom_point() + geom_smooth(method = 'loess', se = FALSE) +
>>     facet_wrap( ~ ID, ncol = 2, scales = 'free_y') +
>>     opts(legend.position = c(0.9, 0.4))
>>
>> This is meant to give you some indication how the two graphics systems
>> work - it's a foundation, not an end product. There are a couple of ways I
>> could have adjusted the y-scales in the lattice graphs (either directly in
>> the scales = ... part or to use a prepanel function for loess), but since
>> you're not likely to use loess in your plots, it's not an important
>> consideration.
>>
>> Both ggplot2 and lattice place the legend outside the plot area by
>> default; I've illustrated a couple of ways to pull it into the plot region
>> FYI.
>>
>> One other thing: if your data set contains fitted values from your PK
>> models for each subject * dose combination, the loess smoothing is
>> unnecessary. In ggplot2, you would use geom_line(aes(y = pred), ...) in
>> place of geom_smooth(), and in lattice you would replace 'smooth' by 'l' in
>> the type = argument of xyplot().
>>
>> HTH,
>> Dennis
>>
>>
>>
>> On Fri, Oct 15, 2010 at 12:46 AM, Anh Nguyen <eatabanana@gmail.com>wrote:
>>
>>> Hello Dennis,
>>>
>>> That's a very good suggestion. I've attached a template here as a .png
>>> file, I hope you can view it. This is what I've managed to achieve in S-Plus
>>> (we use S-Plus at work but I also use R because there's some very good R
>>> packages for PK data that I want to take advantage of that is not available
>>> in S-Plus). The only problem with this is, unfortunately, I cannot figure
>>> out how make the scale non-uniform and I hope to fix that. My data looks
>>> like this:
>>>
>>> ID        Dose         Time         Conc          Pred ...
>>> 1         5               0              0                0
>>> 1         5               0.5           6                8
>>> 1         5               1             16               20
>>> ...
>>> 1         7               0              0                0
>>> 1         7               0.5          10               12
>>> 1         7               1             20               19
>>> ...
>>> 1        10              3             60               55
>>> ...
>>> 2        5                12           4                 2
>>> ...
>>> ect
>>>
>>>
>>> I don't care if it's ggplot or something else as long as it looks like
>>> how I envisioned.
>>>
>>>
>>>
>>>
>>> On Fri, Oct 15, 2010 at 12:22 AM, Dennis Murphy <djmuser@gmail.com>wrote:
>>>
>>>> I don't recall that you submitted a reproducible example to use as a
>>>> template for assistance. Ista was kind enough to offer a potential solution,
>>>> but it was an abstraction based on the limited information provided in your
>>>> previous mail. If you need help, please provide an example data set that
>>>> illustrates the problems you're encountering and what you hope to achieve -
>>>> your chances of a successful resolution will be much higher when you do.
>>>> BTW, there's a dedicated newsgroup for ggplot2:
>>>> look for the mailing list link at  http://had.co.nz/ggplot2/
>>>>
>>>> HTH,
>>>> Dennis
>>>>
>>>>
>>>> On Thu, Oct 14, 2010 at 10:02 PM, Anh Nguyen <eatabanana@gmail.com>wrote:
>>>>
>>>>> I found 2 problems with this method:
>>>>>
>>>>> - There is only one line for predicted dose at 5 mg.
>>>>> - The different doses are 5, 7, and 10 mg but somehow there is a legend
>>>>> for
>>>>> 5,6,7,8,9,10.
>>>>> - Is there a way to make the line smooth?
>>>>> - The plots are also getting a little crowded and I was wondering if
>>>>> there a
>>>>> way to split it into 2 or more pages?
>>>>>
>>>>> Thanks for your help.
>>>>>
>>>>> On Thu, Oct 14, 2010 at 8:09 PM, Ista Zahn <izahn@psych.rochester.edu
>>>>> >wrote:
>>>>>
>>>>> > Hi,
>>>>> > Assuming the data is in a data.frame named "D", something like
>>>>> >
>>>>> > library(ggplot2) # May need install.packages("ggplot2") first
>>>>> > ggplot(D, aes(x=Time, y=Concentration, color=Dose) +
>>>>> > geom_point() +
>>>>> > geom_line(aes(y = PredictedConcentration, group=1)) +
>>>>> > facet_wrap(~ID, scales="free", ncol=3)
>>>>> >
>>>>> > should do it.
>>>>> >
>>>>> > -Ista
>>>>> > On Thu, Oct 14, 2010 at 10:25 PM, thaliagoo <eatabanana@gmail.com>
>>>>> wrote:
>>>>> > >
>>>>> > > Hello-- I have a data for small population who took 1 drug at 3
>>>>> different
>>>>> > > doses. I have the actual drug concentrations as well as predicted
>>>>> > > concentrations by my model. This is what I'm looking for:
>>>>> > >
>>>>> > > - Time vs Concentration by ID (individual plots), with each subject
>>>>> > > occupying 1 plot -- there is to be 9 plots per page (3x3)
>>>>> > > - Observed drug concentration is made up of points, and predicted
>>>>> drug
>>>>> > > concentration is a curve without points. Points and curve will be
>>>>> the
>>>>> > same
>>>>> > > color for each dose. Different doses will have different colors.
>>>>> > > - A legend to specify which color correlates to which dose.
>>>>> > > - Axes should be different for each individual (as some individual
>>>>> will
>>>>> > have
>>>>> > > much higher drug concentration than others) and I want to see in
>>>>> detail
>>>>> > how
>>>>> > > well predicted data fits observed data.
>>>>> > >
>>>>> > > Any help would be greatly appreciated.
>>>>> > > --
>>>>> > > View this message in context:
>>>>> >
>>>>> http://r.789695.n4.nabble.com/Time-vs-Concentration-Graphs-by-ID-tp2996431p2996431.html
>>>>> > > Sent from the R help mailing list archive at Nabble.com.
>>>>> > >
>>>>> > > ______________________________________________
>>>>> > > 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.
>>>>> > >
>>>>> >
>>>>> >
>>>>> >
>>>>> > --
>>>>> > Ista Zahn
>>>>> > Graduate student
>>>>> > University of Rochester
>>>>> > Department of Clinical and Social Psychology
>>>>> > http://yourpsyche.org
>>>>> >
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>
>>
>

	[[alternative HTML version deleted]]

