0

This is related to my earlier SO post. I am trying to plot contours with irregularly spaced data. I have tried three ways of getting a map to overlay (from various SO posts). None of them works for me. I would appreciate help. Here are the three ways I tried: Method 1)

Map from google

rwa <- get_map(location = c(lon = 29.75, lat = -2), zoom = 9, maptype = "toner", source = "stamen")
g1 <- ggmap(rwa)

This shows me the google url and the line below and the link to the terms of service Google Maps API Terms of Service

Then nothing happens;

Method 2

remfname <- "http://biogeo.ucdavis.edu/data/diva/adm/RWA_adm.zip"
locfname <- "/tmp/RWAadm.gz"    
download.file(remfname, locfname)    
con <- gzcon(file(locfname, "rb"))    
myPoints <- readBin(con, numeric(), n=1e6, size = 4, endian = "little")    
close(con)
rwa <- fortify(myPoints)

This gives me the following error:

 int [1:89709] 67324752 20 1446445064 1826437762 49564023 370147328 786432 1464991744 1684102977 1680748653 ...
Error: ggplot2 doesn't know how to deal with data of class integer.

replacing numeric() by integer throws a similar error, while real() is unknown

Method 3

tmpenv <- new.env()
load("C:/R_Progs/Scripts/RWA_adm0.RData", envir=tmpenv, verbose = TRUE)
rwa <- tmpenv$Data
rwa <- fortify(myPoints)

or

rwa <- readOGR(dsn = "C:/R_Progs/Scripts", layer = "RWA_adm0")
rwa <- fortify(myPoints)

Error:
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file 'C:/R_Progs/rwanda/R.RData', probable reason 'No such file or directory'

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 
  Cannot open file

These are my plotting functions

    g1 + geom_tile(data = df, aes(x = Lon, y = Lat, z = Rain, fill = Rain), alpha = 0.8) + stat_contour(data = df, aes(x = Lon, y = Lat, z = Rain)) +
 ggtitle("Rwandan rainfall") + xlab("Longitude") + ylab("Latitude") + scale_fill_continuous(name = "Rain (mm)",
 low = "white", high = "blue") + theme_bw() + theme(plot.title = element_text(size = 25, face = "bold"),
 legend.title = element_text(size = 15), axis.text = element_text(size = 15), axis.title.x = element_text(size = 20, vjust = -0.5),
 axis.title.y = element_text(size = 20, vjust = 0.2),legend.text = element_text(size = 10)) + coord_map()

and

    ggplot() + 
 geom_polygon(data = rwa, aes(x = long, y = lat, group = group),
 colour = "black", size = 0.5, fill = "white") + stat_contour(data = df, aes(x = Lon, y = Lat, z = Rain)) +
 ggtitle("Rwandan rainfall") + xlab("Longitude") + ylab("Latitude") + scale_fill_continuous(name = "Rain (mm)",
 low = "white", high = "blue") + theme_bw() + theme(plot.title = element_text(size = 25, face = "bold"),
 legend.title = element_text(size = 15), axis.text = element_text(size = 15), axis.title.x = element_text(size = 20, vjust = -0.5),
 axis.title.y = element_text(size = 20, vjust = 0.2),legend.text = element_text(size = 10)) + coord_map()

These are the libraries loaded;

library(reshape2)
library(ggplot2)    
library(ggmap)    
library(rgdal)    
library(akima)    
library(directlabels)

and preparation of the data:

datr <- read.table("Apr0130precip.txt",header=TRUE,sep="")
datfr <- data.frame(datr)    
x <- datfr[,1]    
y <- datfr[,2]    
z <- datfr[,3]    
fld <- with(datfr, interp(x , y, z))    
df <- melt(fld$z, na.rm = TRUE)    
names(df) <- c("x", "y", "Rain")    
df$Lon <- fld$x[df$x]    
df$Lat <- fld$y[df$y]

and here is the data:

    Lon Lat Rain

28.92   -2.47   83.4    
29.02   -2.68   144    
29.25   -1.67   134.7    
29.42   -2.07   174.9    
29.55   -1.58   151.5    
29.57   -2.48   224.1    
29.6    -1.5    254.3    
29.72   -2.18   173.9    
30.03   -1.95   154.8    
30.05   -1.6    152.2    
30.13   -1.97   126.2    
30.33   -1.3    98.5    
30.45   -1.81   145.5    
30.5    -2.15   151.3
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Zilore Mumba
  • 1,346
  • 4
  • 23
  • 33
  • In [my answer to your previous question](http://stackoverflow.com/questions/19339296/plotting-contours-on-an-irregular-grid/19339663#19339663) I provided working solutions using google map ('Update 1') and shapefiles ('Update 2'). Can you please clarify what goes wrong when you try them? Cheers. – Henrik Oct 22 '13 at 09:47
  • @henrik thanks the first two methods are from what you gave me. The google maps method works but then no error and nothing happens. The second one from a url at ucdavis downloads and reads the file but then gives error as related to 'readBin(con, numeric(), n=1e6, size = 4, endian = "little")', saying 'ggplot2 doesn't know how to deal with data of class integer', same for integer. Since then I have been going through posts on loading map info. – Zilore Mumba Oct 22 '13 at 10:56
  • The code that I posted works for me. You are _not_ running the same code. The "nothing happens" and "gives error" does not refer to the code that I provided. I don't how I can clarify this further. Sorry. – Henrik Oct 22 '13 at 11:13
  • If you remember in one of my last comments I said I will check on the google temrs of service. Below is what I get when I run your code exactly as you sent it – Zilore Mumba Oct 22 '13 at 15:39
  • removed 1st few lines: GDAL 1.9.2, released 2012/10/08 Path to GDAL shared files: C:/Users/Hp/Documents/R/win-library/3.0/rgdal/gdal GDAL does not use iconv for recoding strings. Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470] Path to PROJ.4 shared files: C:/Users/Hp/Documents/R/win-library/3.0/rgdal/proj Loading required package: grid Loading required package: quadprog Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-2,29.75&zoom=9&size=%20640x640&maptype=terrain&sensor=false Google Maps API Terms of Service : http://developers.google.com/maps/terms – Zilore Mumba Oct 22 '13 at 15:41

0 Answers0