[R] plot.new() and grid functions in multipage pdfs

Paul Murrell paul at stat.auckland.ac.nz
Mon Oct 22 22:40:53 CEST 2012


Hi

Sorry, that should be ...

(i) To start a new page, use plot.new() (if you are trying to mix 'grid' 
and base graphics)

(ii) Watch out for 'grid' and base graphics fighting each other over the 
clipping region (you might need to use grid.clip() after a call to 
plot() or your 'grid' output may not appear - this is the problem in 
your test2.pdf below).

my.plot <- function() {
     plot.new()
     grid.clip()
     pushViewport(viewport(layout = grid.layout(nrow=1, ncol=2)))
     pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
     grid.rect(c(0.25, 0.75), width=c(0.5, 0.5), gp=gpar(fill=c("blue", 
"red")))
     popViewport()

     pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
     par(new=TRUE, fig=gridFIG())
     plot(1:10)
     popViewport()
     popViewport()
}

my.plot();  my.plot()

Did anyone mention that this can get fiddly ...

Paul

On 23/10/12 09:07, Paul Murrell wrote:
> Hi
>
> Here are a couple of suggestions:
>
> (i) To start a new page, use plot.new() FIRST, followed by
> grid.newpage() (if you are trying to mix 'grid' and base graphics)
>
> (ii) Watch out for 'grid' and base graphics fighting each other over the
> clipping region (you might need to use grid.clip() after a call to
> plot() or your 'grid' output may not appear - this is the problem in
> your test2.pdf below).
>
> For your simplified example, something like this should work ...
>
> my.plot <- function() {
>      plot.new()
>      grid.newpage()
>      pushViewport(viewport(layout = grid.layout(nrow=1, ncol=2)))
>      pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
>      grid.rect(c(0.25, 0.75), width=c(0.5, 0.5), gp=gpar(fill=c("blue",
> "red")))
>      popViewport()
>
>      pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
>      par(new=TRUE, fig=gridFIG())
>      plot(1:10)
>      popViewport()
>      popViewport()
> }
>
> my.plot();  my.plot()
>
> Does that translate to your real situation ?
>
> Paul
>
>
> On 20/10/12 13:28, Ali Tofigh wrote:
>> On Wed, Oct 17, 2012 at 4:08 PM, ilai <keren at math.montana.edu> wrote:
>>> On Wed, Oct 17, 2012 at 11:10 AM, Ali Tofigh <alix.tofigh at gmail.com>
>>> wrote:
>>>>
>>>> my problem is that I usually have no choice but to mix grid and base
>>>> graphics.
>>>
>>> What does that have to do with the answer you got ? did you even try
>>> it ?
>>> here it is (again) but this time mixing base+grid:
>>
>> The answer I got was that "base and grid graphics don't usually play
>> well together". I replied that I don't have a choice. So my reply has
>> everything to do with the answer I got. And yes, of course I tried. I
>> tried not using plot.new() and using grid.newpage() instead. Contrary
>> to what you think, that does not always work. Below is a minimal
>> example that
>> you requested.
>>
>> It seems that I have to use a mix of plot.new() and grid.newpage().
>> But if base graphics and grid don't play well together, is there
>> another option?
>>
>> library(grid)
>> library(gridBase)
>>
>> # plot a rectangle on the right side and a simple plot on the left.
>> my.plot <- function() {
>>      pushViewport(viewport(layout = grid.layout(nrow=1, ncol=2)))
>>      pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
>>      grid.rect(c(0.25, 0.75), width=c(0.5, 0.5),
>> gp=gpar(fill=c("blue", "red")))
>>      popViewport()
>>
>>      pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
>>      par(new=TRUE, fig=gridFIG())
>>      plot(1:10)
>>      popViewport()
>>      popViewport()
>> }
>>
>> ## incorrect first plot
>> pdf("test1.pdf")
>> my.plot(); grid.newpage(); my.plot()
>> dev.off()
>>
>> ## incorrect second plot
>> pdf("test2.pdf")
>> plot.new(); my.plot(); plot.new(); my.plot()
>> dev.off()
>>
>> ## this works as intended with a mix of plot.new() and grid.newpage
>> pdf("test3.pdf")
>> plot.new(); my.plot(); grid.newpage(); my.plot()
>> dev.off()
>>
>> /ali
>>
>>>
>>> require(gridBase)
>>> pdf("test.pdf")
>>>
>>> grid.rect(gp = gpar(fill="blue"))
>>> grid.newpage()
>>> grid.rect(gp=gpar(fill='blue'))
>>> # mix in base+grid. adapted from ?gridPAR in gridBase
>>> par(fig=gridFIG(), new=TRUE)
>>>   plot(1:10)
>>> # plot.new()  # uncomment to see it's unnecessary
>>> plot(1:10)
>>>   pushViewport(viewport(width=0.5, height=0.5)) ;
>>> grid.rect(gp=gpar(col="grey", lwd=2))
>>> plot(rnorm(10))
>>> grid.newpage()
>>> grid.rect(gp=gpar(fill='blue'))
>>> dev.off()
>>>
>>>>
>>>> I use grid as much as possible, but for example for plotting
>>>> dendrograms, I don't know how to plot them other than using base
>>>> graphics. So I use the functions in gridBase to produce those plots.
>>>
>>>
>>> Then you may have noticed the dendrogram examples in the gridBase
>>> docs don't
>>> use plot.new() either but use lattice for the layout.
>>>
>>>
>>>> In order to do that I have to call plot.new() at some point in my code
>>>> to initialize the base graphics, and that can mess things up.
>>>
>>>
>>>>
>>>> No. See example above or provide a minimal reproducible example that
>>>> does
>>>> require it.
>>>
>>>
>>>
>>>>
>>>> /ali
>>>>
>>>>
>>>> On Tue, Oct 9, 2012 at 4:00 PM, Greg Snow <538280 at gmail.com> wrote:
>>>>> The plot.new function is for base graphics and base and grid graphics
>>>>> don't usually play well together.  You probably want to use
>>>>> grid.newpage function instead.
>>>>>
>>>>> On Tue, Oct 9, 2012 at 1:26 PM, Ali Tofigh <alix.tofigh at gmail.com>
>>>>> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> when using the grid package, I've come across this weird behaviour
>>>>>> where a call to plot.new() will start a new page for a multi-page
>>>>>> pdf,
>>>>>> but then the margins will somehow behave strangely for all but the
>>>>>> first page: here is some code:
>>>>>>
>>>>>> pdf("test.pdf"); plot.new(); grid.rect(gp = gpar(fill="blue"));
>>>>>> plot.new();  grid.rect(gp = gpar(fill="blue")); dev.off()
>>>>>>
>>>>>> The first page is filled completely with a blue rectangle, but in the
>>>>>> second page, the margins clip the rectangle. This is causing me
>>>>>> considerable headache, as I rely on many grid functions for plotting.
>>>>>> This seems like a bug to me, or is there something about the
>>>>>> behaviour
>>>>>> of plot.new() and/or grid functions that I don't understand?
>>>>>>
>>>>>> /Ali
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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.
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Gregory (Greg) L. Snow Ph.D.
>>>>> 538280 at gmail.com
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>>
>>
>> ______________________________________________
>> 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/




More information about the R-help mailing list