From baptiste.auguie at googlemail.com  Mon Dec 14 16:09:15 2009
From: baptiste.auguie at googlemail.com (baptiste auguie)
Date: Mon, 14 Dec 2009 16:09:15 +0100
Subject: [Rd] raster support in graphics devices
In-Reply-To: <4B25A7EC.8090102@auckland.ac.nz>
References: <4B146EC6.2000502@auckland.ac.nz>
	<de4e29f50912011038v33c3503dmb732dbd0d8ffd456@mail.gmail.com>
	<4B15A5C0.4060400@auckland.ac.nz>
	<de4e29f50912031310j598729f3g9a188d6a270cb91c@mail.gmail.com>
	<4B1C138C.5080204@auckland.ac.nz>
	<de4e29f50912061259y283eb4abp14bb3f1637d6113@mail.gmail.com>
	<4B25A7EC.8090102@auckland.ac.nz>
Message-ID: <de4e29f50912140709m53032a93na70fde820bb086a0@mail.gmail.com>

Hi,

[my message below is a bit off-topic]

2009/12/14 Paul Murrell <p.murrell at auckland.ac.nz>:

> I have thought a bit about drawing the same image multiple times (more in
> the context of using a bitmap for a plotting symbol). ?I imagine something
> like being able to "add" a raster image to a device and then simply "refer"
> to that image (e.g., by name) is needed.

That would be nice.

I first thought that this new rasterGrob would be a good way to
implement some kind of filling pattern for grid graphics, but after
some more testing I was surprised to see that a similar approach with
vector patterns could result in more efficient, better output. Of
course raster filling patterns could still be nice to have for fancy
images or gradients.

One thing that worries me with the approach I've taken below is that
the grob is not vectorized. In particular, if one was to use it as a
building block for histograms (as in base graphics' density parameter)
each rectangle would have to be drawn in sequence. I can of course
Vectorize() this grob into a new one (gTree) but I haven't seen any
examples like this before. Does this sound reasonable? (argument
explosion might be another problem...)

Best regards,

baptiste

Below is the gist of my experiments (the first part is not related to
grid.raster and runs in current R).


library(grid)

## vector patterns
source("http://gridextra.googlecode.com/svn/trunk/R/vpattern.r")

dev.new()
grid.newpage()
grid.vpattern(x=0.2, width=unit(3,"cm"), height=unit(0.5,"npc"),
             motif="points", motif.params=list(pch=22,
                               gp=gpar(cex=0.8, lwd=5, col="grey90",
fill="grey95")), clip=T)

grid.vpattern(x=0.5, width=unit(0.3,"npc"), height=unit(0.5,"npc"),
             motif="segments", motif.params=list(angle=45,
                                 gp=gpar(lty=2, col="grey", lwd=2.4)))

grid.vpattern(x=0.5, y=0.9, width=unit(0.5,"npc"), height=unit(1,"cm"),
             motif="points", motif.params=list(pch="+"), clip=F,
pattern.offset=c(1, 0.5))

grid.vpattern(x=0.8,  width=unit(0.2,"npc"), height=unit(0.2,"npc"),
             motif="grid", motif.params=list(angle=45,
                             gp=gpar(lwd=10, col="grey90")), clip=T,
pattern.offset=c(-1, 0.5))

grid.vpattern(x=0.5, y=0.1, width=unit(0.9,"npc"), height=unit(0.1,"npc"),
             motif="grid", motif.params=list(angle=30, gp=gpar(col="red")),
              gp=gpar(alpha=0.1, fill="red"))

## raster patterns

source("http://gridextra.googlecode.com/svn/trunk/R/rpattern.r")

## a raster motif
plus <- function(..., width=5, height=5){
  x11(width=width/25.4, height=height/25.4)
  grid.points(x=unit(0.5, "npc"), y=unit(0.5, "npc"),
              pch=3, size=unit(2, "mm"), gp=gpar(), ...)
  m <- grid.cap()
  dev.off()
  invisible(m)
}
.plus <- plus()

g1 <- rpatternGrob(x=0.5, y=0.4, width=unit(3.7, "cm"),
                   height=unit(0.4, "npc"),
                   clip=TRUE, motif=.plus)

dev.new()
grid.draw(g1)



[Rd] raster support in graphics devices
baptiste auguie