Skip to content
Advertisement

Loading a raster data of map PNG on ggmaps of R

This will be cross posted on R’s mailing list.

I have the map as a png, so I won’t be using the get_map function. I have extracted the raster data from the png, and I wish to load the map as it is on the display of R, and then I would like to plot a point on it.

So, here’s the way I have tried ggmaps. The program is compiling fine. Problem here is that there isn’t any output being shown.

library (png)
library (ggmap)

latitude  = c(40.702147,40.718217,40.711614)
longitude = c(-74.012318,-74.015794,-73.998284)

# Reads a PNG and outputs a raster array.
img <- readPNG (system.file ("img", "My.png", package="png"))

df <- data.frame (latitude, longitude)

# img: raster array read from the map png.
ggimage (img, fullpage = TRUE) + geom_point (data = df, aes_auto (df), size = 2)

qplot (latitude, longitude, data = df, colour = I("red"), size = I(3))

Of course I am doing something wrong. Please point out.

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggmap_2.1     ggplot2_0.9.1 png_0.1-4    

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.1       
 [5] labeling_0.1       MASS_7.3-18        memoise_0.1        munsell_0.3       
 [9] plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1    
[13] RgoogleMaps_1.2.0  rjson_0.2.8        scales_0.2.1       stringr_0.6       
[17] tools_2.15.1      
> 

EDIT: I have found an error. Actually I was first running it with source (uff.R), and this command didn’t show any error. Then I tried Rscript.

anisha@linux-y3pi:~> Rscript uff.R
Loading required package: ggplot2
Loading required package: methods
Error in eval(expr, envir, enclos) : object 'x' not found
Calls: print ... sapply -> lapply -> eval.quoted -> lapply -> FUN -> eval
Execution halted

Advertisement

Answer

Your ggimage is failing because there’s no x and y in it. Rename your lat-long coords to x and y. Here is a completely reproducible example. This is basic ggplot stuff:

> library(png)
> library(ggplot2)
> img <- readPNG(system.file("img", "Rlogo.png", package="png"))
> latitude  = c(40.702147,40.718217,40.711614)
> longitude = c(-74.012318,-74.015794,-73.998284)
> df <- data.frame (x=longitude,y=latitude)
> qplot(x,y,data = df, colour = I("red"), size = I(3))

Run those commands on your command line and you should see a plot. Possible reasons for failing are:

  • Your R doesn’t have an X11 connection to your display. Is this all running on a local Linux machine? You haven’t connected to a server? If R can’t pop up a graphics window it will probably try and create an Rplots.pdf file.
  • You are running in a script which isn’t printing. Wrap all ggplot, grid, and lattice graphics functions that you want to produce output in print() function calls. This is a FAQ, I think. Paul Hiemstra put this as an answer but then deleted it…
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement