[R] FW: making a plot in xyplot

Ordaz, Sarah ordazs at upmc.edu
Wed Nov 4 14:32:02 CET 2009


Thank you!

Sarah Ordaz

Laboratory of Neurocognitive Development

University of Pittsburgh

ordazs at upmc.edu

 


-----Original Message-----
From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com] 
Sent: Friday, October 30, 2009 2:44 AM
To: Ordaz, Sarah
Cc: r-help at r-project.org; spector at stat.berkeley.edu
Subject: Re: [R] FW: making a plot in xyplot

On Mon, Oct 26, 2009 at 12:19 PM, Ordaz, Sarah <ordazs at upmc.edu> wrote:
> Hi,
> I'd like to clarify my previous posting.  See below - my updates are noted with *s
> Thanks,
> Sarah Ordaz
> ordazs at upmc.edu
>
> Hello,
>
> I am a newbie to the lattice package in R, and I'm trying to make a plot using the xyplot function.  I have repeated
> measures data (2 conditions) for two different groups of subjects (teens and adults).
>
> So far, I've made a basic graph using xyplot(y ~x, group=subnum, data=mydata, type="b").
> *y is cognitive performance
> *x is pupil diameter
> *subnum = subject number
> *agegrp = age group (either teens or adults)
> *Here is "mydata", a sample data set:
>
> subnum  agegrp condition  pupil(x)   cognitive(y)
> 101           T         A          5.4            97
> 101           T        B          4.4            98
> 102           T         A          3.1            88
> 102           T         B          4.9            95
> 103           A         A          5.3            75
> 103           A         B          4.2            83
> 104           A         A          5.8            84
> 104           A         B           6.1            65
>
>
> Now I would like to make all the teens' lines one color and the adults' another.  Additionally, I'd like to reformat the dots so
> that the points referencing condition A and condition B are different.  Can you help me with this?


## you need to define an interaction, and then limit the number of
## colors to two (recycling ensures that the colors are associated
## with agegrp):

xyplot(y ~ x, group=interaction(agegrp, subnum),
       data=mydata, type="b", auto.key = TRUE,
       par.settings = simpleTheme(col = c("blue", "green")))

## for a more appropriate legend:

xyplot(y ~ x, group=interaction(agegrp, subnum),
       data=mydata, type="b", auto.key = list(text = c("A", "T")),
       par.settings = simpleTheme(col = c("blue", "green")))

## for different plotting characters, you need to do a bit more work:

xyplot(y ~ x, group=interaction(agegrp, subnum),
       data=mydata,
       par.settings = simpleTheme(col = c("blue", "green")),
       panel = panel.superpose,
       panel.groups = function(x, y, type, pch, ...) {
           panel.xyplot(x, y, type = "l", ...)
           panel.xyplot(x[1], y[1], type = "p", pch = 16, ...)
           panel.xyplot(x[2], y[2], type = "p", pch = 17, ...)
       })

-Deepayan




More information about the R-help mailing list