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

Patrick Nicholson patrick.nicholson at asranalytics.com
Sat May 26 23:09:11 CEST 2012


Thanks Uwe, I had tried this before but hearing it again made me realize what I was doing incorrectly. I still can't get it to work with grid.draw(), but here is the solution with ggplot():


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()
________________________________________
From: Uwe Ligges [ligges at statistik.tu-dortmund.de]
Sent: Saturday, May 26, 2012 11:43 AM
To: Patrick Nicholson
Cc: r-help at R-project.org
Subject: Re: [R] R base package grid does not output raster image

On 24.05.2012 23:04, Patrick Nicholson wrote:
> 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?

This is a known issue with the Remote Desktop Connection. The raster
images are not shown via that setup. It works if you work locally at the
server or is you plot into another device (i.e. a pdf file or so).

Best,
Uwe Ligges


>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list