R基本包格不产生输出(R base package grid does not produce ou

2019-06-24 17:47发布

我在Windows Server 2008 R2 Amazon EC2实例上运行64位R 2.15.0。 grid不产生输出。 例如,下面的代码应该产生在设备窗口的单个对角线:

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

但是,我什么也看不见。 有没有我应该使用基于Windows Server 2008 R2,使电网输出的标志或选择?

编辑:上我家中的另一种重复的例子(Windows 7的64位)和办公PC(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)

这是预期输出,看到我的工作电脑上(调整大小以适应下图):

Answer 1:

dev.list()可以调用返回打开的图形设备的命名载体。 在Windows上,例如:

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

dev.cur()将返回当前的活动设备。 如果没有设备打开,你可以打开一个:

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

对于PDF,你必须要确保关闭设备或者PDF文件将不会呈现:

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

?device帮助页面列出了其他图形设备。 通常调用grid.newpage()如果没有打开自动打开一个新的设备,但也许不是你的情况。 以上的例子为我工作在Windows 7 64位和Ubuntu 11.10 64位。

@attitude_stool:是否有上述帮助找出问题了吗?



Answer 2:

R不超过远程桌面连接的窗口的设备正确地产生的光栅图像。 如果需要的光栅图像,积必须被输出到另一装置。

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

我发现,写之间的绘图命令pdf()dev.off()产生空白文件。 存储对象的情节和调用它会工作。



文章来源: R base package grid does not produce output