5

I'm trying to add this plot of a function defined on Veneto (italian region) enter image description here

obtained by an image and contour:

image(X,Y,evalmati,col=heat.colors(100), xlab="", ylab="", asp=1,zlim=zlimits,main=title)
contour(X,Y,evalmati,add=T)

(here you can find objects: https://dl.dropboxusercontent.com/u/47720440/bounty.RData)

on a Google Map background.

I tried two ways:

PACKAGE RGoogleMaps

I downloaded the map mbackground

MapVeneto<-GetMap.bbox(lonR=c(10.53,13.18),latR=c(44.7,46.76),size = c(640,640),MINIMUMSIZE=TRUE)
PlotOnStaticMap(MapVeneto)

but i don't know the commands useful to add the plot defined by image and contour to the map

PACKAGE loa

I tried this way:

lat.loa<-NULL
lon.loa<-NULL
z.loa<-NULL
nx=dim(evalmati)[1]
ny=dim(evalmati)[2]
for (i in 1:nx)
{
    for (j in 1:ny)
    {
        if(!is.na(evalmati[i,j]))
        {
            lon.loa<-c(lon.loa,X[i])
            lat.loa<-c(lat.loa,Y[j])
            z.loa<-c(z.loa,evalmati[i,j])
        }
    }
}

GoogleMap(z.loa ~ lat.loa*lon.loa,col.regions=c("red","yellow"),labels=TRUE,contour=TRUE,alpha.regions=list(alpha=.5, alpha=.5),panel=panel.contourplot)

but the plot wasn't like the first one: enter image description here

in the legend of this plot I have 7 colors, and the plot use only these values. image plot is more accurate.

How can I add image plot to GoogleMaps background?

Community
  • 1
  • 1
Darko
  • 1,448
  • 4
  • 27
  • 44
  • I tried to deliver an answer using `ggmap`. But, what you are asking seems to be difficult to achieve. I saved your ideal image as png and used `inset_raster` in `ggmap` to overlay the png. This of course failed since you overlay the entire png file on top of the googlemap image. If you can somehow crop the relevant part, you may have a chance to achieve your outcome. Benoit's answer seems to be the way to go. – jazzurro Jan 15 '15 at 03:31

2 Answers2

5

I am not realy inside the subject, but Lovelace, R. "Introduction to visualising spatial data in R" might help you https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf From section "Adding base maps to ggplot2 with ggmap" with small changes and data from https://github.com/Robinlovelace/Creating-maps-in-R/archive/master.zip

library(dplyr)
library(ggmap)
library(rgdal)   

lnd_sport_wgs84 <- readOGR(dsn = "./Creating-maps-in-R-master/data", 
                           layer = "london_sport") %>%
  spTransform(CRS("+init=epsg:4326")) 

lnd_wgs84_f <- lnd_sport_wgs84 %>%
  fortify(region = "ons_label") %>%
  left_join(lnd_sport_wgs84@data, 
            by = c("id" = "ons_label"))

ggmap(get_map(location = bbox(lnd_sport_wgs84) )) + 
  geom_polygon(data = lnd_wgs84_f,
               aes(x = long, y = lat, group = group, fill = Partic_Per),
               alpha = 0.5)

enter image description here

ckluss
  • 1,477
  • 4
  • 21
  • 33
  • 1
    this plot coloured the sub-regions of London, but how can I give a color to each point, like `image` doES? – Darko Jan 12 '15 at 17:19
5

If the use of a GoogleMap map is not mandatory (e.g. if you only need to visualize the coastline + some depth/altitude information on the map), you could use the package marmap to do what you want. Please note that you will need to install the latest development version of marmap available on github to use readGEBCO.bathy() since the format of the files generated when downloading GEBCO files has been altered recently. The data from the NOAA servers is fine but not very accurate in your region of interest (only one minute resolution vs half a minute for GEBCO). Here is the data from GEBCO I used to produce the map : GEBCO file

library(marmap)

# Get hypsometric and bathymetric data from either NOAA or GEBCO servers
# bath <- getNOAA.bathy(lon1=10, lon2=14, lat1=44, lat2=47, res=1, keep=TRUE)
bath <- readGEBCO.bathy("GEBCO_2014_2D_10.0_44.0_14.0_47.0.nc")

# Create color palettes for sea and land
blues <- c("lightsteelblue4", "lightsteelblue3", "lightsteelblue2", "lightsteelblue1")
greys <- c(grey(0.6), grey(0.93), grey(0.99))

# Plot the hypsometric/bathymetric map
plot(bath, land=T, im=T, lwd=.03, bpal = list(c(0, max(bath), greys), c(min(bath), 0, blues)))
plot(bath, n=1, add=T, lwd=.5)  # Add coastline

# Transform your data into a bathy object
rownames(evalmati) <- X
colnames(evalmati) <- Y
class(evalmati) <- "bathy"

# Overlay evalmati on the map
plot(evalmati, land=T, im=T, lwd=.1, bpal=col2alpha(heat.colors(100),.7), add=T, drawlabels=TRUE) # use deep= shallow= step= to adjust contour lines
plot(outline.buffer(evalmati),add=TRUE, n=1) # Outline of the data

# Add cities locations and names
library(maps)
map.cities(country="Italy", label=T, minpop=50000)

Since your evalmati data is now a bathy object, you can adjust its appearance on the map like you would for the map background (adjust the number and width of contour lines, adjust the color gradient, etc). plot.bath() uses both image() and contour() so you should be able to get the same results as when you plot with image(). Please take a look at the help for plot.bathy() and the package vignettes for more examples.

enter image description here

Benoit
  • 1,154
  • 8
  • 11
  • I need to see also cities in the map... Is this possible? – Darko Jan 14 '15 at 14:12
  • Since this is a base plot, you can add any text/information on the existing map. For instance, you can use `map.cities(country="Italy", label=TRUE, minpop=50000)` from package `maps`to add on the map the location and name of cities bigger than 50 thousand inhabitants. – Benoit Jan 14 '15 at 16:22
  • This package allows only this type of map, or also others (road map, ecc..)? – Darko Jan 14 '15 at 18:15
  • 1
    The main purpose of `marmap` is to ease the creation of bathymetric maps. Functions allow the easy downloading and import of bathymetric data from several public repositories (i.e. GEBCO, NOAA...). We didn't include functions to overlay roads, rivers or other geographic features. But as explained above, maps produced by `marmap` are base graphics. So you can add data layers using any base graphics functions from any package. The challenge is to find appropriate data (in a compatible format) to improve your maps. Best. – Benoit Jan 14 '15 at 19:39
  • http://cran.r-project.org/web/packages/marmap/index.html is the CRAN page for marmap: "Import, Plot and Analyze Bathymetric and Topographic Data -- Import xyz data from the NOAA, GEBCO and other sources, plot xyz data to prepare publication-ready figures, analyze xyz data to extract transects, get depth / altitude based on geographical coordinates, or calculate z-constrained least-cost paths." – Dave X May 12 '15 at 20:11