2

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 produce a single diagonal line in a device window:

grid.newpage()
l <- linesGrob()
grid.draw(l)

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

EDIT: Another reproducible example that works on my home (Windows 7 x64) and work PCs (Windows XP):

library(grid)
library(png)

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

grid.draw(background)

This is the expected output, as seen on my work PC (resized to fit below):

R-log-png

attitude_stool
  • 1,023
  • 1
  • 13
  • 18
  • 1
    I am not familiar with Windows images on EC2, but when running linux instances, the default graphics device, I think, is pdf, which saves the output to Rplots.pdf. see `?Device`, and try `options("device")`. On linux instances, I'll often forward X11 via ssh if I want to be more interactive with the plots (although this can sometimes be slow). Otherwise, I'll wrap `pdf(file = "file1.pdf")` and `dev.off()` around the call to `plot()` or `grid.draw()`, and access the pdf later via usual means of viewing an image file on a remote server (sftp, ftp, etc.). – jthetzel May 23 '12 at 21:56
  • Does plot(1) do what you expect? – mdsumner May 23 '12 at 23:12
  • @jthetzel Wrapping the plot between a save command and off just results in a blank image. – attitude_stool May 23 '12 at 23:57
  • @mdsumner `plot(1)` works as expected – attitude_stool May 23 '12 at 23:58
  • Put the details in the q to improve it, currently it is incomplete – mdsumner May 24 '12 at 00:16

2 Answers2

2

dev.list() can be called to return a named vector of open graphics devices. On Windows, for example:

windows()
pdf()
dev.list()
# windows     pdf 
#       2       3 
dev.off(); dev.off()
dev.list()
# NULL

And dev.cur() will return the currently active device. If there are no devices open, you can open one:

windows()
grid.newpage()
l <- linesGrob()
grid.draw(l)

For pdf, you have to be sure to close the device or else the pdf file will not render:

pdf() # plot saved by default to Rplots.pdf
grid.newpage()
l <- linesGrob()
grid.draw(l)
dev.off() 

The ?device help page lists the other graphics devices. Usually a call to grid.newpage() automatically opens a new device if none are open, but perhaps not in your case. The above examples work for me on Windows 7 x64 and Ubuntu 11.10 x64.

@attitude_stool: Does any of the above help identify your problem?

jthetzel
  • 3,603
  • 3
  • 25
  • 38
  • @jhetzel Thank you for the response, but this doesn't address the problem or give me any clues to its cause. I added a better example to the original question. My actual objective is using `grid` to draw a .png background to a `ggplot2` graph. I can copy the code back and forth between my personal and work PCs and have it run perfectly. On EC2, I get the `ggplot2` part (the actual graph), but the background is blank. – attitude_stool May 24 '12 at 13:35
  • @attitude_stool: Which device are you using? Have you tried using the pdf or png devices, and then copying the resulting image file back to your home or work computers? This could help verify that the problem is with the grid package on the EC2 instance, as opposed to a problem with the Windows device or PDF viewer on EC2. – jthetzel May 24 '12 at 13:42
  • @jhetzel Why would the rest of the plot render if the device was the problem? In any case, I tried all of the devices and got blank output for all. – attitude_stool May 24 '12 at 16:21
  • @attitude_stool: Fair point. Perhaps you can post `sessionInfo()`, though I don't think it will be revealing. Does `grid:::drawGrob(l)` yield anything? Does lattice work fine: `xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width | Species, data = iris)` – jthetzel May 24 '12 at 18:15
  • @attitude_stool: One more thing: any luck with `print(grid.draw(l))` ? – jthetzel May 24 '12 at 19:17
  • @jhetzel This answer was generally correct. I will add a full answer. – attitude_stool May 26 '12 at 20:43
2

R does not produce raster images correctly in the window device over Remote Desktop Connection. If a raster image is required, the plot must be output to another device.

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))

p <- 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"))

pdf("plot.pdf", height=7, width=7)
p
dev.off()

I have found that writing plotting commands between the pdf() and dev.off() produces blank files. Storing the plot in an object and calling it will work.

attitude_stool
  • 1,023
  • 1
  • 13
  • 18
  • 1
    you don't need to mess with `panel.background` to have a background image, especially with no grid lines. You can simply use `?annotation_raster` or `?annotation_custom` instead. – baptiste May 26 '12 at 21:44