Simon Kiss
2011-Oct-31 16:21 UTC
[R] Help combining cell labelling and multiple mosaic plots
Dear colleagues
I'm using data that looks like .test and .test1 below to draw two mosaic
plots with cell labelling (the row percentages from the tables).
When I take out the pop=FALSE commands in the mosaic commands and comment out
the two lines labelling the cells, then the plots are laid out exactly as
I'd like: side-by-side.
But I do require the cell labelling and the pop=FALSE arguments. I suspect I
need to add in a call to pushViewport or an upViewport command, but I'm not
sure. Any advice is welcome.
library(vcd)
library(grid)
.test<-as.table(matrix(c(1, 2, 3, 4, 5, 6), nrow=3, ncol=2, byrow=TRUE))
.test<-prop.table(.test, 1)
.test1<-as.table(matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow=TRUE))
.test1<-prop.table(.test1, 1)
dimnames(.test)<-list("Fluoride Cluster"=c('Beneficial\nand
Safe', 'Mixed Opinion', 'Harmful With No Benefits'),
"Governments Should Not Impose Treatment"=c('Agree',
'Disagree'))
dimnames(.test1)<-list("Vaccines Are Too Much To Handle"=
c('Agree' , 'Disagree'), "Governments Should Not Oblige
Treatment" =c('Agree', 'Disagree'))
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
pushViewport(viewport(layout.pos.col=1))
mosaic(.test, gp=shading_hsv, pop=FALSE,
split_verticaL=FALSE, newpage=FALSE,
labeling_args=list(offset_varnames=c(top=3), offset_labels=c(top=2)))
labeling_cells(text=round(prop.table(.test, 1), 2)*100, clip=FALSE)(.test)
popViewport()
pushViewport(viewport(layout.pos.col=2))
mosaic(.test1, gp=shading_hsv, newpage=FALSE,pop=FALSE, split_vertical=FALSE,
labeling_args=list(offset_varnames=c(top=3), offset_labels=c(top=2)))
labeling_cells(text=round(prop.table(.test1, 1), 2)*100, clip=FALSE)(.test1)
popViewport(2)
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 905 746 7606
Paul Murrell
2011-Nov-07 01:26 UTC
[R] Help combining cell labelling and multiple mosaic plots
Hi
The problem is that BOTH mosaic() and labeling_cells() are calling
seekViewport() to find the right viewport to draw into and for BOTH
plots they are finding the same viewports (on the left side of the
page). The following code solves the problem (for me anyway) by
specifying a different 'prefix' for each mosaic() and labeling_cells()
call ...
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
pushViewport(viewport(layout.pos.col=1))
mosaic(.test, gp=shading_hsv, pop=FALSE, split_verticaL=FALSE,
newpage=FALSE,
labeling_args=list(offset_varnames=c(top=3),
offset_labels=c(top=2)),
prefix="plot1")
labeling_cells(text=round(prop.table(.test, 1), 2)*100,
clip=FALSE)(.test, prefix="plot1")
upViewport()
pushViewport(viewport(layout.pos.col=2))
mosaic(.test1, gp=shading_hsv, newpage=FALSE, pop=FALSE,
split_vertical=FALSE,
labeling_args=list(offset_varnames=c(top=3),
offset_labels=c(top=2)),
prefix="plot2")
labeling_cells(text=round(prop.table(.test1, 1), 2)*100,
clip=FALSE)(.test1, prefix="plot2")
popViewport(2)
... hope that helps.
Paul
On 1/11/2011 5:21 a.m., Simon Kiss wrote:> Dear colleagues I'm using data that looks like .test and .test1 below
> to draw two mosaic plots with cell labelling (the row percentages
> from the tables). When I take out the pop=FALSE commands in the
> mosaic commands and comment out the two lines labelling the cells,
> then the plots are laid out exactly as I'd like: side-by-side. But I
> do require the cell labelling and the pop=FALSE arguments. I suspect
> I need to add in a call to pushViewport or an upViewport command, but
> I'm not sure. Any advice is welcome.
>
>
> library(vcd) library(grid)
>
>
> .test<-as.table(matrix(c(1, 2, 3, 4, 5, 6), nrow=3, ncol=2,
> byrow=TRUE)) .test<-prop.table(.test, 1) .test1<-as.table(matrix(c(1,
> 2, 3, 4), nrow=2, ncol=2, byrow=TRUE)) .test1<-prop.table(.test1, 1)
>
> dimnames(.test)<-list("Fluoride
Cluster"=c('Beneficial\nand Safe',
> 'Mixed Opinion', 'Harmful With No Benefits'),
"Governments Should Not
> Impose Treatment"=c('Agree', 'Disagree'))
> dimnames(.test1)<-list("Vaccines Are Too Much To Handle"=
c('Agree' ,
> 'Disagree'), "Governments Should Not Oblige Treatment"
=c('Agree',
> 'Disagree')) grid.newpage()
> pushViewport(viewport(layout=grid.layout(1,2)))
> pushViewport(viewport(layout.pos.col=1)) mosaic(.test,
> gp=shading_hsv, pop=FALSE, split_verticaL=FALSE, newpage=FALSE,
> labeling_args=list(offset_varnames=c(top=3),
> offset_labels=c(top=2))) labeling_cells(text=round(prop.table(.test,
> 1), 2)*100, clip=FALSE)(.test) popViewport()
>
> pushViewport(viewport(layout.pos.col=2)) mosaic(.test1,
> gp=shading_hsv, newpage=FALSE,pop=FALSE, split_vertical=FALSE,
> labeling_args=list(offset_varnames=c(top=3),
> offset_labels=c(top=2))) labeling_cells(text=round(prop.table(.test1,
> 1), 2)*100, clip=FALSE)(.test1) popViewport(2)
> ********************************* Simon J. Kiss, PhD Assistant
> Professor, Wilfrid Laurier University 73 George Street Brantford,
> Ontario, Canada N3T 2C9 Cell: +1 905 746 7606
>
> ______________________________________________ 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.
--
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/