[R] two questions about xyplot

William Shadish wshadish at ucmerced.edu
Mon Sep 23 20:29:49 CEST 2013


Dear Richard, your solution to the second question worked like a charm. 
Thanks! So much to learn about this stuff, but at least it is fun.

On the first question, yes, I want a text to display the mean in each of 
the 12 panels.

Will

On 9/23/2013 11:23 AM, Richard Kwock wrote:
> Hi,
>
> To answer your second question you can do something like this:
>
> p<-xyplot(dvy ~ sessidx | case, group = numph, data=d66df, col = c(1:4),
>      layout=c(1, 3), xlab= "Sessions",
>      ylab = "Number of Seconds",
>      type="l")
>
> update(p, panel=function(...){
>          panel.xyplot(...)
>          panel.abline(v=6.5)
>          panel.abline(v=12.5)
>          panel.abline(v=18.5)
> } )
>
> By setting the "group" parameter in xyplot to be "numph",  xyplot will
> plot different lines for each group of numph you have in each case.
>
> For your first question, did you mean you want a text to display the
> mean in each panel?
>
> Richard
>
>
> On Mon, Sep 23, 2013 at 10:41 AM, William Shadish <wshadish at ucmerced.edu> wrote:
>> Dear R helpers,
>>
>> I am generating three artificial short interrupted time series datasets
>> (single-case designs; call them Case 1, Case 2, Case 3) and then plotting
>> them in xyplot. I will put the entire code below so you can reproduce. I
>> have been unable to figure out how to do two things.
>>
>> 1. Each time series has 24 time points divided into four phases. Call them
>> phases A, B, C, D for convenience. I have used running() to compute the
>> means of the observations in each of these four parts; and I saved these as
>> objects called mn1 (for Case 1), mn2 (Case 2) and mn3 (Case 3). So mn1
>> contains the four means for A, B, C, D phases for Case 1, etc. I want to
>> insert these means into the xyplot in the appropriate place. For instance,
>> insert the first mean from mn1 into phase A of Case 1, the second mean into
>> phase B of Case 1, and so forth until insert the fourth mean from mn3 into
>> phase D of Case 3. Ideally, it would insert something like "M = 49.02" or
>> "Xbar = 49.02" into phase A for Case 1.
>>
>> 2. The xyplot code I use creates a line connecting the data points, and that
>> line is continuous over the entire graph. I would like to have the lines be
>> discontinuous between phases. Phase changes are indicated by panel.abline()
>> in the code, and occur at time points 6.5, 12.5, and 18.5. So, for example,
>> I would like a line connecting the datapoints from 1 to 6, then 7-12, then
>> 13-18, then 19-24 (but not including 6-7, 12-13, and 18-19).
>>
>> I appreciate any help you might be able to offer.
>>
>> Will Shadish
>>
>> Here is the code:
>> #############################################################################
>> library(gtools)
>> library(lattice)
>> ###g = .66
>> z <- rnorm(24, mean = 0, sd = 10)
>> w <- rnorm(24, mean = 0, sd = 10)
>> ###change mean = to vary the effect size
>> tm <- rnorm(6, mean = 10, sd = 10)
>> b <- rep(0,6)
>> c <- rep(1,6)
>> tmt <- c(b,tm,b,tm)
>> for (t in 2:24) z[t] <- 0.25 * z[t - 1] + w[t]
>> dvy <- 50 + z + tmt
>> jid <- rep(1,24)
>> sid <- rep(1,24)
>> pid <- rep(1,24)
>> dvid <- rep(1,24)
>> desvar <- rep(1,24)
>> dvdir <- rep(0,24)
>> sessidx <- c(1:24)
>> m <- rep(1,6)
>> n <- rep(2,6)
>> o <- rep(3,6)
>> p <- rep(4,6)
>> numph <- c(m,n,o,p)
>> phasebtm <- c(b,c,b,c)
>> d1 <- cbind(jid,sid,pid,dvid,desvar,dvdir,dvy,sessidx,numph,phasebtm)
>> mn1 <- running(dvy, width=6, by=6)
>>
>> #second dataset
>> z <- rnorm(24, mean = 0, sd = 10)
>> w <- rnorm(24, mean = 0, sd = 10)
>> tm <- rnorm(6, mean = 10, sd = 10)
>> b <- rep(0,6)
>> c <- rep(1,6)
>> tmt <- c(b,tm,b,tm)
>> for (t in 2:24) z[t] <- 0.25 * z[t - 1] + w[t]
>> dvy <- 50 + z + tmt
>> jid <- rep(1,24)
>> sid <- rep(1,24)
>> pid <- rep(2,24)
>> dvid <- rep(1,24)
>> desvar <- rep(1,24)
>> dvdir <- rep(0,24)
>> sessidx <- c(1:24)
>> m <- rep(1,6)
>> n <- rep(2,6)
>> o <- rep(3,6)
>> p <- rep(4,6)
>> numph <- c(m,n,o,p)
>> phasebtm <- c(b,c,b,c)
>> d2 <- cbind(jid,sid,pid,dvid,desvar,dvdir,dvy,sessidx,numph,phasebtm)
>> mn2 <- running(dvy, width=6, by=6)
>>
>> #third dataset
>> z <- rnorm(24, mean = 0, sd = 10)
>> w <- rnorm(24, mean = 0, sd = 10)
>> tm <- rnorm(6, mean = 10, sd = 10)
>> b <- rep(0,6)
>> c <- rep(1,6)
>> tmt <- c(b,tm,b,tm)
>> for (t in 2:24) z[t] <- 0.25 * z[t - 1] + w[t]
>> dvy <- 50 + z + tmt
>> jid <- rep(1,24)
>> sid <- rep(1,24)
>> pid <- rep(3,24)
>> dvid <- rep(1,24)
>> desvar <- rep(1,24)
>> dvdir <- rep(0,24)
>> sessidx <- c(1:24)
>> m <- rep(1,6)
>> n <- rep(2,6)
>> o <- rep(3,6)
>> p <- rep(4,6)
>> numph <- c(m,n,o,p)
>> phasebtm <- c(b,c,b,c)
>> d3 <- cbind(jid,sid,pid,dvid,desvar,dvdir,dvy,sessidx,numph,phasebtm)
>> mn3 <- running(dvy, width=6, by=6)
>>
>> #concatenate d1 d2 d3
>> d66 <- rbind(d1, d2, d3)
>> d66df <- as.data.frame(d66)
>> d66df$case <- ordered(d66df$pid,
>> levels = c(1,2,3),
>> labels = c("Case 3", "Case 2", "Case 1"))
>> p<-xyplot(dvy ~ sessidx | case, data=d66df,
>>      layout=c(1, 3), xlab= "Sessions",
>>      ylab = "Number of Seconds",
>>      type="l")
>> update(p, panel=function(...){
>>          panel.xyplot(...)
>>          panel.abline(v=6.5)
>>          panel.abline(v=12.5)
>>          panel.abline(v=18.5)
>> } )
>>
>> --
>> William R. Shadish
>> Distinguished Professor
>> Founding Faculty
>>
>> Mailing Address:
>> William R. Shadish
>> University of California
>> School of Social Sciences, Humanities and Arts
>> 5200 North Lake Rd
>> Merced CA  95343
>>
>> Physical/Delivery Address:
>> University of California Merced
>> ATTN: William Shadish
>> School of Social Sciences, Humanities and Arts
>> Facilities Services Building A
>> 5200 North Lake Rd.
>> Merced, CA 95343
>>
>> 209-228-4372 voice
>> 209-228-4007 fax (communal fax: be sure to include cover sheet)
>> wshadish at ucmerced.edu
>> http://faculty.ucmerced.edu/wshadish/index.htm
>> http://psychology.ucmerced.edu
>>
>> ______________________________________________
>> 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.
>

-- 
William R. Shadish
Distinguished Professor
Founding Faculty

Mailing Address:
William R. Shadish
University of California
School of Social Sciences, Humanities and Arts
5200 North Lake Rd
Merced CA  95343

Physical/Delivery Address:
University of California Merced
ATTN: William Shadish
School of Social Sciences, Humanities and Arts
Facilities Services Building A
5200 North Lake Rd.
Merced, CA 95343

209-228-4372 voice
209-228-4007 fax (communal fax: be sure to include cover sheet)
wshadish at ucmerced.edu
http://faculty.ucmerced.edu/wshadish/index.htm
http://psychology.ucmerced.edu



More information about the R-help mailing list