Google map from dismo :: gmap () and ggplot2

I got a map with the dismo::gmap() function and I want to build it with ggplot2 because I want to add different functions using the geom_point functions and other ggplot functions. I prefer to use dismo::gmap instead of ggmap::get_map() to load the google map layer. This is because dismo::gmap() , unlike ggmap::get_map() , returns a raster layer from a raster package, including full CRS information, and therefore it should be possible to change the projection of the layer.

 > head(data_info$latitude, 20) #[1] 49.11306 49.39333 48.78083 51.85000 53.57361 50.67806 52.69083 52.21389 53.46361 50.99917 53.99750 53.54528 53.61417 48.00556 48.01306 53.45000 #[17] 51.93667 54.53083 51.95500 54.29639 > head(data_info$longitude, 20) #[1] 13.134722 12.323056 13.803889 12.177778 14.143611 13.175833 12.649444 13.454167 11.629722 10.906111 11.415556 8.426944 7.160000 11.123889 10.786111 #[16] 12.766667 11.987222 13.091389 10.967500 13.684167 e = extent(-14 , 58 , 28 , 64) mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off") mapImageData2_proj <- projectExtent(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84") # plot the points on the map ggplot(mapImageData2_proj, extent = "device") + geom_point(inherit.aes = FALSE, aes(x = data_info$longitude, y = data_info$latitude), data = gps, colour = "red", size = 1, pch = 20) 

After that, I get the following error:

Error: ggplot2 does not know how to handle RasterLayer class data

If I try this one

 plot(mapImageData2_proj) 

Error in .plotraster2 (x, col = col, maxpixels = maxpixels, add = add ,: there are no values ​​associated with this RasterLayer

+7
r google-maps ggplot2
source share
2 answers

There are two problems with this question. One of them is how to get ggplot2 to draw a Raster * object. Another way is to reprogram the raster by storing its values.

OP contains code

 library(dismo) e = extent(-14 , 58 , 28 , 64) mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off") 

If we run this and then do plot(mapImageData2) , we get a good plot. Then OP starts

 mapImageData2_proj <- projectExtent(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84") 

Now, if we run plot(mapImageData2_proj) , we get an error! This is because projectExtent returns a RasterLayer with no values. Instead, we should use projectRaster() . See ?projectExtent more details. So we run:

 mapImageData2_proj <- projectRaster(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84") plot(mapImageData2_proj) 

Now we see the redesigned display. Progress! But we still cannot build mapImageData2_proj with ggplot2 , because ggplot2 does not know how to handle the Raster * object. We need to convert our raster file to a data frame. There are several ways to do this, but without downloading any additional packages, raster::rasterToPoints() is a good option. For example:

 myPoints <- raster::rasterToPoints(myRaster) myDataFrame <- data.frame(myPoints) colnames(myDataFrame) <- c("Longitude", "Latitude", "Values") ggplot(data=myDataFrame, aes_string(y = "Latitude", x = "Longitude")) + geom_raster(aes(fill = Values)) 

So, to put it all together in the OP example:

 library(dismo) e = extent(-14 , 58 , 28 , 64) mapImageData2 <- gmap(e, type = c("terrain"), lonlat = TRUE, path = "&style=feature:all|element:labels|visibility:off&style=feature:administrative.country|element:geometry.stroke|visibility:off") plot(mapImageData2) mapImageData2_proj <- projectRaster(mapImageData2, crs = "+proj=utm +zone=31 +datum=WGS84") plot(mapImageData2_proj) myRaster <- mapImageData2_proj myPoints <- raster::rasterToPoints(myRaster) myDataFrame <- data.frame(myPoints) colnames(myDataFrame) <- c("X", "Y", "Values") ggplot(data=myDataFrame, aes_string(y = "Y", x = "X")) + geom_raster(aes(fill = Values)) 
+5
source share

To directly build a Raster * object in ggplot , you can use the RasterVis library as follows:

 r <- raster(system.file("external/test.grd", package="raster")) s <- stack(r, r*2) names(s) <- c('meuse', 'meuse x 2') library(ggplot2) theme_set(theme_bw()) gplot(s) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) + scale_fill_gradient(low = 'white', high = 'blue') + coord_equal() 

As you can see, we use gplot , not ggplot . This function is intended for Raster * objects, since it allows you to load only part of the Raster * object into RAM. You can select the maximum number of pixels to load on the map using gplot(s, maxpixels=50000) .
In fact, I recommend not turning your raster into dots, because if your raster array is huge, you won’t be able to load it into RAM ...

+4
source share

All Articles