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()
会产生空白文件。将绘图存储在对象中并调用它会起作用。