[R] gridBase and new.page() / grid.newpage()
Paul Murrell
p.murrell at auckland.ac.nz
Sun Aug 10 23:54:42 CEST 2008
Hi
Peter Cowan wrote:
> Paul,
>
> The real case is a function where I'd want to allow end users to pass
> an arbitrary plotting function to be a sub plot within a larger plot
> (e.g. the code that does the density plots would actually be passed as
> a parameter. I adapted my example to reflect this. I expect most of
> my end users to be unfamiliar with grid, yet familiar with base
> graphics.
Ok. Thanks for the clarification.
You may be buying yourself a fair amount of pain with that goal (base
graphics functions are NOT generally written to play nicely with even
other base graphics functions, let alone other graphics systems), but
for your particular example, here's a possible fix:
- remove the grid.newpage() call (the plot.new() takes care of starting
a new page)
- add 'clip="off"' to the first pushViewport() call in myplot(); the
problem with the bottom-right subplot is due to clipping regions not
being reset correctly.
myplot() becomes ...
myplot <- function(subplot) {
plot.new()
n <- 10
datalayout <- grid.layout(ncol = 2, width = unit(c(1, 1/n),
c('null', 'null')))
pushViewport(viewport(width = .8, height = .8, layout = datalayout,
clip="off"))
grid.rect(gp=gpar(col="grey"))
for(i in 1:n) {
pushViewport(viewport(
y = i/n - 0.5 / n, x = 1,
height = unit(1/n, 'npc'),
width = unit(1/n, 'npc')))
subplot()
upViewport()
}
pushViewport(viewport(xscale = c(0, 1), layout.pos.col = 1))
grid.rect(gp=gpar(fill="grey"))
grid.points()
upViewport()
upViewport()
}
... does that fix things for you ?
Paul
> Thanks,
>
> Peter
>
> Aplot <- function(){
> par(plt = gridPLT(), new = TRUE)
> grid.rect(gp=gpar(fill="light grey"))
> dens <- density(rnorm(10))
> plot.density(dens, axes = FALSE,
> mar = c(0,0,0,0), main = "", xlab = "", ylab = "")
> }
>
> myplot <- function(subplot) {
> grid.newpage()
> plot.new()
> n <- 10
> datalayout <- grid.layout(ncol = 2, width = unit(c(1, 1/n),
> c('null', 'null')))
>
> pushViewport(viewport(width = .8, height = .8, layout = datalayout))
> grid.rect(gp=gpar(col="grey"))
>
> for(i in 1:n) {
> pushViewport(viewport(
> y = i/n - 0.5 / n, x = 1,
> height = unit(1/n, 'npc'),
> width = unit(1/n, 'npc')))
> subplot()
> upViewport()
> }
> pushViewport(viewport(xscale = c(0, 1), layout.pos.col = 1))
> grid.rect(gp=gpar(fill="grey"))
> grid.points()
> upViewport()
> upViewport()
> }
>
> myplot(Aplot)
> myplot(Aplot)
>
> pdf()
> myplot(Aplot)
> myplot(Aplot)
> dev.off()
>
>
> On Sun, Aug 10, 2008 at 1:48 PM, Paul Murrell <p.murrell at auckland.ac.nz> wrote:
>> Hi
>>
>>
>> Peter Cowan wrote:
>>> Hello all,
>>>
>>> I'm trying to write a function using the gridBase package. I'd like
>>> to push several base subplots to a larger plot constructed with grid.
>>> However, I'm having trouble getting consistent results when running
>>> the function when the plotting window (quartz) is closed, when it is
>>> left open and the plot function is repeated to the same window, and
>>> when the output is saved to a pdf, see the example below. In the case
>>> of the pdf plot pages are preceded by a blank page. However, leaving
>>> out either plot.new() or grid.newpage() each presents different
>>> problems. I looked through the R Graphics book and package vignette
>>> but didn't see this particular issue addressed.
>>
>> There can be complications when both the 'graphics' package and the
>> 'grid' package try to start a new page.
>>
>> Before trying to untangle this particular example, is it possible to
>> side step the problem by drawing those density plots using a
>> grid-compatible system such as either lattice or ggplot ?
>>
>> Or, if the "real" case is as simple as this, perhaps even just
>> grid.lines() will do the job (?)
>>
>> Paul
>>
>>
>>> Thanks in advance,
>>>
>>> Peter
>>>
>>> myplot <- function() {
>>> grid.newpage()
>>> plot.new()
>>> n <- 10
>>> datalayout <- grid.layout(ncol = 2, width = unit(c(1, 1/n),
>>> c('null', 'null')))
>>>
>>> pushViewport(viewport(width = .8, height = .8, layout = datalayout))
>>> grid.rect(gp=gpar(col="grey"))
>>>
>>> for(i in 1:n) {
>>> pushViewport(viewport(
>>> y = i/n - 0.5 / n, x = 1,
>>> height = unit(1/n, 'npc'),
>>> width = unit(1/n, 'npc')))
>>> par(plt = gridPLT(), new = TRUE)
>>> grid.rect(gp=gpar(fill="light grey"))
>>> dens <- density(rnorm(10))
>>> plot.density(dens, axes = FALSE,
>>> mar = c(0,0,0,0), main = "", xlab = "", ylab = "")
>>> upViewport()
>>> }
>>> pushViewport(viewport(xscale = c(0, 1), layout.pos.col = 1))
>>> grid.rect(gp=gpar(fill="grey"))
>>> grid.points()
>>> upViewport()
>>> upViewport()
>>> }
>>>
>>> myplot()
>>> myplot()
>>>
>>> pdf()
>>> myplot()
>>> myplot()
>>> dev.off()
>>>
>>> ______________________________________________
>>> 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/
>>
--
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 R-help
mailing list