[R] R base package grid does not output raster image

Patrick Nicholson patrick.nicholson at asranalytics.com
Thu May 24 23:04:09 CEST 2012


I am running 64-bit R 2.15.0 on a Windows Server 2008 R2 Amazon EC2 instance. grid does not produce output. For example, the following code should print the R logo to the window() device:


library(grid)
library(png)

img.path <- system.file("img", "Rlogo.png", package="png")
bg <- readPNG(img.path)
background <- rasterGrob(unclass(bg))

grid.draw(background)


I, however, see nothing. Is there a flag or option I should be using on Windows Server 2008 R2 to enable grid output?

For clarity, the following is how I am actually using images in R plots. This code works on my laptop (R 2.15.0, ggplot2 0.8.9):


library(ggplot2)
library(grid)
library(maps)
library(mapproj)
library(png)
library(RgoogleMaps)

counties <- map_data("county", region="virginia")
states <- map_data("state")

tmp <- tempfile(fileext=".png")
bg <- GetMap.bbox(range(counties$long), range(counties$lat), destfile=tmp, maptype="satellite", format="png32")
background <- readPNG(tmp)
background <- rasterGrob(unclass(background))

ggplot(counties, aes(long, lat)) + 
  coord_map(xlim=c(bg$BBOX$ll[2], bg$BBOX$ur[2]), ylim=c(bg$BBOX$ll[1], bg$BBOX$ur[1])) +
  geom_path(aes(group=group), color="darkgrey") +
  geom_path(data=states, aes(group=group), color="white", size=1) +
  opts(axis.line=theme_blank(), 
       axis.text.x=theme_blank(), 
       axis.text.y=theme_blank(),
       axis.ticks=theme_blank(), 
       axis.title.x=theme_blank(), 
       axis.title.y=theme_blank(),
       axis.ticks.length=unit(0, "lines"), 
       axis.ticks.margin=unit(0, "lines"),
       panel.border=theme_blank(), 
       panel.background=function(...)background,
       panel.grid.major=theme_blank(), 
       panel.grid.minor=theme_blank(),
       panel.margin=unit(0, "lines"), 
       legend.position="none", 
       legend.title=theme_blank(),
       legend.background=theme_blank(),
       plot.margin=unit(0*c(-1.5, -1.5, -1.5, -1.5), "lines"))


Thank you,
Patrick



More information about the R-help mailing list