lattice and base-graphics using gridBase
1
0
Entering edit mode
@maarten-van-iterson-3028
Last seen 9.6 years ago
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? 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 -- Maarten van Iterson Center for Human and Clinical Genetics Leiden University Medical Center (LUMC) Research Building, Einthovenweg 20 Room S-04-038 Phone: 071-526 9439 E-mail: M.van_iterson.HG at lumc.nl --------------- Postal address: Postzone S-04-P Postbus 9600 2300 RC Leiden The Netherlands
• 1.4k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 5 hours ago
United States
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. 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 > > -- James W. MacDonald, M.S. Biostatistician Douglas Lab University of Michigan Department of Human Genetics 5912 Buhl 1241 E. Catherine St. Ann Arbor MI 48109-5618 734-615-7826 ********************************************************** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues
ADD COMMENT
0
Entering edit mode
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/
ADD REPLY
0
Entering edit mode
Hi Paul Murrell, Thanks for your reply this is exactly what I was looking for. To answer your question: I prefer the default graphical parameters of the boxplot-function and tried to reproduce these using the bwplot (and trellis.par.set) but didn't succeed and thought that combining both was maybe easier? In the future I will post questions like these on the R-help listserv. Cheers, Maarten On Wed, 2010-05-12 at 16:42 +1200, Paul Murrell wrote: > 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 > >> > >> > > > -- Maarten van Iterson Center for Human and Clinical Genetics Leiden University Medical Center (LUMC) Research Building, Einthovenweg 20 Room S-04-038 Phone: 071-526 9439 E-mail: M.van_iterson.HG at lumc.nl --------------- Postal address: Postzone S-04-P Postbus 9600 2300 RC Leiden The Netherlands
ADD REPLY

Login before adding your answer.

Traffic: 769 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6