[R] Overlaying lattice graphs

hadley wickham h.wickham at gmail.com
Tue Jun 12 22:51:21 CEST 2007


On 6/12/07, Seb <pomchip at free.fr> wrote:
> Hello
>
> I apologize in advance if this question has already be posted on the
> list, although I could not find a relevant thread in the archives.
>
> I would like to overlay xyplots using different datasets for each plot.
> I typically work on the following data.frame (mydata) structure
>
> >mydata
>         Drug    Time        Observed          Predicted
> 1       A        0.05         10                 10.2
> 2       A        0.10         20                 19.5
> etc...
> 100     B        0.05         11                 12.7
> 101     B        0.10         35                 36
> etc...
>
> I want to plot the observed data as points and the predicted values as
> lines. If I use the following commands, I don't have the possibility to
> switch the "y" values from Observed for the scatterplot to Predicted for
> the line.
>
> xyplot(Observed ~ Time | Drug, data = mydata, panel  =  function(x,y, ...){
> +            panel.xyplot(x,y,...)
> +            panel.xyplot(x,y,type="l",...)})
>
> I wonder if this problem can be solved using the trellis.focus "family"
> commands but I have a hard time to understand how they work.

Another approach would be to use ggplot, http://had.co.nz/ggplot2.
Then your code might look something like:

ggplot(mydata, aes(x=Time)) +
geom_point(aes(y=Observed)) +
geom_line(aes(y = Predicted)) +
facet_grid(. ~ Drug)

Hadley



More information about the R-help mailing list