[R] bwplot [lattice]: how to get different y-axis scales for each row?

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Apr 1 16:43:23 CEST 2011


On Sun, Mar 27, 2011 at 8:50 PM, Marius Hofert <m_hofert at web.de> wrote:
> Dear expeRts,
>
> I partially managed to obtain what I wanted by using latticeExtra. However, the
> following questions remain:
> 1) why do not all x-axis labels appear? [compare bw and bw2]

You have unnecessarily asked for relation="free" for the x-axis (even
though they don't change across panels), and then tried to combine
them using combineLimits(), which doesn't know how to handle
categorical axes.

Change to using relation="free" for the y-axis only, and you should be fine.

bw <- bwplot(error ~ methods | attr * groups, data=df,
            as.table=TRUE, notch=TRUE,
            scales = list(y = list(alternating = c(1,1), tck=c(1,0),
                                   relation = "free")))


> 2) Can I have the y-axis labels on the right margin/side of the plot? Changing
> the "alternating" argument does not do the job since relation="free"

No. (Well, you can provide your own axis() function to do it, in which
case you will need to allocate space too.)

-Deepayan



> Cheers,
>
> Marius
>
>
>
> library(lattice)
> library(latticeExtra)
>
> ## build example data set
> dim <- c(100, 6, 4, 3) # n, groups, methods, attributes
> dimnames <- list(n=paste("n=", seq_len(100), sep=""),
>                 groups=paste("group=", seq_len(6), sep=""),
>                 methods=paste("method=", seq_len(4), sep=""),
>                 attr=paste("attribute=", seq_len(3), sep=""))
> set.seed(1)
> data <- rexp(prod(dim))
> arr <- array(data=data, dim=dim, dimnames=dimnames)
> arr[,2,,] <- arr[,2,,]*10
> arr[,4,2,2] <- arr[,4,2,2]*10
> z <- abs(sweep(arr, 3, 1))
> df <- as.data.frame.table(z, responseName="error")
>
> ## box plot
> bw <- bwplot(error ~ methods | attr * groups, data=df,
>             as.table=TRUE, notch=TRUE,
>             scales=list(y=list(alternating=c(1,1), tck=c(1,0)),
>             relation="free"))
> (bw2 <- useOuterStrips(combineLimits(bw, extend=FALSE))))
>
>
>
> On 2011-03-26, at 09:34 , Marius Hofert wrote:
>
>> Dear expeRts,
>>
>> How can I get ...
>> (1) different y-axis scales for each row
>> (2) while having the same y-axis scales for different columns?
>>
>> I coulnd't manage to do this with relation="free" [which gives (1) but not (2)].
>> I also tried relation="sliced", but it did not give the same y-axis scales
>> within each row (see the fourth row). Further, it "separates" the panels.
>>
>> Cheers,
>>
>> Marius
>>
>> ## minimal example:
>>
>> library(lattice)
>>
>> ## build example data set
>> dim <- c(100, 6, 2, 3) # n, groups, methods, attributes
>> dimnames <- list(n=paste("n=", seq_len(100), sep=""),
>>                groups=paste("group=", seq_len(6), sep=""),
>>                methods=paste("method=", seq_len(2), sep=""),
>>                attr=paste("attribute=", seq_len(3), sep=""))
>> set.seed(1)
>> data <- rexp(prod(dim))
>> arr <- array(data=data, dim=dim, dimnames=dimnames)
>> arr[,2,,] <- arr[,2,,]*10
>> arr[,4,2,2] <- arr[,4,2,2]*10
>> z <- abs(sweep(arr, 3, 1))
>> df <- as.data.frame.table(z, responseName="error")
>>
>> ## box plot
>> bwplot(error ~ methods | attr * groups, data=df,
>>       as.table=TRUE, notch=TRUE,
>>       scales=list(alternating=c(1,1), tck=c(1,0)))
>>
>> ## with relation="sliced"
>> bwplot(error ~ methods | attr * groups, data=df,
>>       as.table=TRUE, notch=TRUE,
>>       scales=list(alternating=c(1,1), tck=c(1,0), relation="sliced"))
>>
>
> ______________________________________________
> 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