geotheory
geotheory

Reputation: 23650

Combining OpenStreetMap and geometric plotting in R

I'm trying to combine {OpenStreetMap} tiles with geometries from other data, but this is proving difficult both in base R and ggplot. The goal is to simply plot some points on the map. Grateful for an idea what I'm missing, or what alternative methods (perhaps with other libraries) would produce the same result.

Thanks in advance.

require(OpenStreetMap)
require(ggplot2)

lat <- c(63, 48); lon <- c(-20, 8)
map <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),4,'osm')
d <- data.frame(x = sample((lon[1]:lon[2]), 10, replace=T), 
                y = sample((lat[1]:lat[2]), 10, replace=T))

# base R plot method
plot(map)
points(d)

# ggplot method
ggplot() + autoplot(map)
ggplot() + autoplot(map) + geom_point(aes(x=d$x, y=d$y))

Upvotes: 2

Views: 2016

Answers (1)

Ian Fellows
Ian Fellows

Reputation: 17348

The raster map you are plotting is in a mercator projection. You can either transform the image to long-lat, or transform you data into the mercator projection. see: help("openproj"). Here is the former:

# base R plot method
mapLL <- openproj(map)
plot(mapLL)
points(d)

Upvotes: 4

Related Questions