R base package grid does not produce output

梦想的初衷 提交于 2019-12-19 10:28:55

问题


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


回答1:


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?




回答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.



来源:https://stackoverflow.com/questions/10728190/r-base-package-grid-does-not-produce-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!