I couldn't help myself: used this picture and adapted this example from Paul Murrell. People who want do this sort of thing might find this link from the R wiki useful as well, although it's a little older and doesn't take advantage of the new(ish) raster capabilities. This post is an example of putting ggplot
graphics in a rounded-corner frame of sorts.
edit: lots of help from Baptiste.
library(png)
library(grid)
imgfile <- "http://upload.wikimedia.org/wikipedia/commons/e/e1/Tie-dye.png"
download.file(imgfile,dest="tiedye.png")
r <- readPNG("tiedye.png")
rmat <- matrix(rgb(r[,,1],r[,,2],r[,,3],alpha=0.4),
nrow=dim(r)[1])
Function for shadowed points:
shadow.points <- function(x, y, size=unit(1, "char"), default.units="native", ...) {
if(!is.unit(x)) {x <- unit(x, default.units) }
if(!is.unit(y)) { y <- unit(y, default.units) }
grid::grid.points(x+0.2*size, y-0.2*size, size=size, gp=gpar(col="black"), pch=20)
grid::grid.points(x, y, size=size, default.units=default.units, ...)
}
Set up mask based on grid.roundrect
:
png("mask.png",width=ncol(r), height=nrow(r), res=1)
grid.roundrect(gp=gpar(fill="black"))
dev.off()
m <- readPNG("mask.png", native=FALSE)
mask <- matrix(rgb(m[,,1],m[,,2],m[,,3]),
nrow=dim(m)[1])
rmat[mask == "#FFFFFF"] <- "#FFFFFF"
(Watch out, I think there is some variation in the support for per-pixel variation in transparency across platforms (e.g. this may not work on Windows??)) warning: there may be artifacts across other platforms, too -- the background didn't show up on a PNG, I had to export as PDF ...
grid.newpage()
pushViewport(plotViewport(),
viewport(xscale=c(0, 10), yscale=c(0, 10)))
grid.raster(rmat,x=unit(0,"native"),y=unit(0,"native"),
width=1,height=1,just=c(0,0))
grid.roundrect() ## frame
grid.xaxis(at=seq(2,8,by=2)) ## axes -- shorter to avoid going beyond end of frame
grid.yaxis(at=seq(2,8,by=2))
shadow.points(x=rnorm(10,mean=5),y=rnorm(10,mean=5),pch=20,
gp=gpar(col="cyan"))
