4

I want to add a basemap to my plot, which visualizes three SpatialPointDataFrames. I've already tried the maptools as well as RgoogleMaps package, but both don't work in the way, which I want to.
My problem: The SpatialPointDataFrames are not drawn on the GoogleMaps Background map.

A minimal example:

The city.csv with the following example content:

FID,city,POINT_X,POINT_Y
0,New York,-73.996786,40.720813
1,Newark,-74.172237, 40.732196

The R Code:

# Load packages
library(RgoogleMaps)
library(sp)

# load .csv file 
city= read.csv("city.csv", header = TRUE)

# convert to SpatialPointDataFrame
coordinates(city) <- c("POINT_X", "POINT_Y")
proj4string(city) <- CRS("+proj=longlat +datum=WGS84")

# use RgoogleMaps
gc <- geocode('new york, usa')
center <- as.numeric(gc)
ggmap(get_googlemap(center = center, color = 'bw', scale = 4), fullpage = T)
# Plot the city dataset
plot(city, pch = 22, col="black", bg= "yellow", cex = 1.5, add = TRUE)

The result should be a plot with the background map and the two points, but the points are not drawn on the map. Is there a geocoding problem or do I miss anything? Is it possible to combine ggmap and plt functions?

Any help is much appreciated!

schlomm
  • 551
  • 2
  • 11
  • 22

3 Answers3

4

Using ggplot2 for this kind of work is much easier, you can add points, polygons, 2densities, etc... to ggmap layers.

library(RgoogleMaps)
library(sp)
library(ggplot2)
library(ggmap)

P is the SpatialPointsDataFrame object:

DB <- data.frame(FID=P$FID, city=P$city)
DB <- cbind(DB, P@coords)


DB <- data.frame(FID=c(0,1), city=c("New York", "Newark"),   POINT_X=c(-73.996786,-74.172237), POINT_Y=c(40.720813,40.732196 ))
gc <- geocode("new york, usa")
center <- as.numeric(gc)
G <- ggmap(get_googlemap(center = center, color = 'bw', scale = 4), extent = "device")
G1 <- G + geom_point(aes(x=POINT_X, y=POINT_Y ),data=DB, color="red", size=5)
plot(G1)

This is the output:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
eclark
  • 819
  • 7
  • 16
  • Thanks for your answer. Unfortunately I have still some problems to include my SpatialPointDataFrame. I want to assign DB to a SpatialPointDataFrame by using "DB <- city, where city is a SpatialPointDataFrame. Then I get two errors: Firstly I get an error from line "G <- ggmap...": Warning message: fullpage and expand syntaxes deprecated, use extent. And after this another error from "G1 <- G + geom_point...": Error: ggplot2 doesn't know how to deal with data of class SpatialPointsDataFrame. – schlomm Feb 28 '14 at 23:53
  • Ok. For the warning (fullpage is deprecated) just change `fullpage=T` for `extent="device"`. The second problem you have if that the grid graphics dont know how to extract the spatial coordinates to plot, so lets convert from SpatialPolygonsDataFrame to data.frame. I've edited the orginal answer to meet this changes. – eclark Mar 01 '14 at 00:06
  • Thanks a lot. Works like a charm :) Two question left: 1.) So...is it not possible to use SpatialPointDataFrames with ggplot2 and I have to use tradidional data.frames? 2.) Is there any good reference for designing a map using ggplot2? Like a short overview about possible parameters like symbology type etc.? Until now I only know how to set the color and size. – schlomm Mar 01 '14 at 00:26
  • 1) Not that I know of (but I could be wrong), so I usually convert them to a dataframe. In the case of SPointsDF its usually pretty straight forward but when you use SPolygons(shapefiles) you have a few extra steps you can learn [here](https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles). 2) I think you can get some cool things out of [this](http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf) and [this](http://spatial.ly/2012/02/great-maps-ggplot2/). ggmap works a lot like ggplot2 so you can add most ggplot2 parameters which you can find in many online tutorials. – eclark Mar 01 '14 at 00:57
  • Thanks for your help :) Very helpful for the future but for now I only use points...luckily :P – schlomm Mar 01 '14 at 01:05
1

These are two different frameworks (grid and base-graphics). Though, a combination of both may be possible, I would recommend to stick with ggplot.

You can easily add the points using geom_point(), see the answers here for example.

Community
  • 1
  • 1
EDi
  • 13,160
  • 2
  • 48
  • 57
0

More recently, other than ggplot2, there are also packages like leaflet and mapview that can handle this. In the case of Leaflet for R, see:

prusswan
  • 6,853
  • 4
  • 40
  • 61