[BioC] lattice and base-graphics using gridBase

Paul Murrell p.murrell at auckland.ac.nz
Wed May 12 06:42:47 CEST 2010


Hi

On 5/12/2010 12:54 AM, James W. MacDonald wrote:
> Hi Maarten,
>
> Maarten van Iterson wrote:
>> Dear list,
>>
>> I'm trying to combine lattice and base-graphics following the example
>> from Paul Murrell's R Graphics (B.1.2).
>>
>> The question I have is how to add the y-axis from the base-graphics to
>> the lattice plot below?
>
> Neither of these packages are part of Bioconductor, so I think you would
> be better served asking this question on the R-help listserv.

Agreed, but since we're here already ...

This code should work:

boxplot.panel <- function(x, y, subscripts, ...){	
       par(plt=gridPLT(), new=TRUE, ps=8)
       boxplot(data[[subscripts]], col=rep(2:7, each=2),
               ylab="", axes=FALSE,
               names=NULL, outline=FALSE)
       axis(2)
}

df <- data.frame(y=1:3, x=1:3, Method=LETTERS[1:3])

plot.new()
xyplot(y ~ x | Method, data=df, subscripts=TRUE, xlab="", ylab="",
        scales=list(draw=FALSE),
        panel=boxplot.panel, layout=c(1,3))

Note that I have taken out the pushing and popping of viewports (so that 
when the boxplot axis is drawn it lines up with the panel border) and 
added axis(2) to draw the axis (and specified 'scales' in xyplot() to 
suppress the lattice axes).  I have also added plot.new() just before 
the call to xyplot(), which makes sure that the base graphics system is 
set up (so you don't get the warning).

But one question for you:  why do you want to use boxplot() instead of 
bwplot() ?

Paul

> Best,
>
> Jim
>
>
>>
>> library(gridBase)
>> library(lattice)
>>
>> data<- list(A=matrix(rnorm(8*100, 1, 1), ncol=8), B=matrix(rnorm(8*100,
>> 0, 1), ncol=8), C=matrix(rnorm(8*100, -1, 1), ncol=8))
>>
>> boxplot.panel<- function(x, y, subscripts, ...){	
>> 	pushViewport(viewport(gp=gpar(fontsize=8)),
>>                 viewport(y=unit(0.95, "npc"), width=0.9,
>> height=unit(0.95, "npc"), just="top"))
>>    par(plt=gridPLT(), new=TRUE, ps=8)
>> 	boxplot(data[[subscripts]], col=rep(2:7, each=2), ylab="", axes=FALSE,
>> names=NULL, outline=FALSE)
>>    popViewport(2)
>> }
>>
>> df<- data.frame(y=1:3, x=1:3, Method=LETTERS[1:3])
>>
>> xyplot(y ~ x | Method, data=df, subscripts=TRUE, xlab="", ylab="",
>> panel=boxplot.panel, layout=c(1,3))
>>
>> sessionInfo()
>> R version 2.10.0 (2009-10-26)
>> x86_64-unknown-linux-gnu
>>
>> locale:
>>   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>>   [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>>   [5] LC_MONETARY=C              LC_MESSAGES=en_US.UTF-8
>>   [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
>>   [9] LC_ADDRESS=C               LC_TELEPHONE=C
>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] grid      stats     graphics  grDevices utils     datasets
>> methods
>> [8] base
>>
>> other attached packages:
>> [1] lattice_0.17-26 gridBase_0.4-3
>>
>> Thanks in advance,
>>
>> Maarten
>>
>>
>

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the Bioconductor mailing list