[R] two questions about xyplot

Richard Kwock richardkwock at gmail.com
Mon Sep 23 22:08:08 CEST 2013


Hi,

Getting text to show on the panel plots is a bit trickier, but doable.

# append to the dataset the mean for each group and line
d66df_mns <- cbind(d66df, "Means" = c(rep(c(mn1, mn2, mn3), each = 6)))

# set the y_lim to extend a bit further above the graph to allow for
the means to be displayed
p<-xyplot(dvy ~ sessidx | case, group = numph, data=d66df_mns, col = c(1:4),
    layout=c(1, 3), xlab= "Sessions",
    ylab = "Number of Seconds", ylim = c(min(d66df_mns$dvy), 110),
    type="l")

# pass in the means as an argument to the panel function
update(p, panel=function(x, y, means = d66df_mns$Means, ... ){
        # print(list(...))

        # this will store the groups index by subscript into a variable
        grps <- list(...)$groups[list(...)$subscript]
        unique_indices <- !duplicated(grps)

        # this will get the mean for each panel and for each line
        mean_1 <- (means[list(...)$subscript][unique_indices])
        print(mean_1)
        print(x[unique_indices])

        panel.xyplot(x, y, ... )
        panel.abline(v=6.5)
        panel.abline(v=12.5)
        panel.abline(v=18.5)
        panel.abline(v=18.5)

       # print the mean values here.
       panel.text(x[unique_indices], 100, paste("M = " , round(mean_1,
2)), adj = c(0,0))
} )

If you are working in lattice a lot, print(list(...)) is a handy
function that will show you what parameters values you are passing in
as "..." in the panel function.

Hope that helps.

Richard

On Mon, Sep 23, 2013 at 11:23 AM, Richard Kwock <richardkwock at gmail.com> 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.



More information about the R-help mailing list