Plot coordinates on map
As an alternative to RgoogleMaps
, you can also use the combination ggplot2
with ggmap
.
With this code:
# loading the required packageslibrary(ggplot2)library(ggmap)# creating a sample data.frame with your lat/lon pointslon <- c(-38.31,-35.5)lat <- c(40.96, 37.5)df <- as.data.frame(cbind(lon,lat))# getting the mapmapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4, maptype = "satellite", scale = 2)# plotting the map with some points on itggmap(mapgilbert) + geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) + guides(fill=FALSE, alpha=FALSE, size=FALSE)
you get this result:
Another option is using the leaflet package (as suggested here). Unlike Google Maps it does not require any API key.
install.packages(c("leaflet", "sp"))library(sp)library(leaflet)df <- data.frame(longitude = runif(10, -97.365268, -97.356546), latitude = runif(10, 32.706071, 32.712210))coordinates(df) <- ~longitude+latitudeleaflet(df) %>% addMarkers() %>% addTiles()
An other alternative, is the plotGoogleMaps
package which allows to plot in a navigator, allowing to zoom in and out etc. You can then make a screenshot of your picture to save it (though remember google maps are legally supposed to be used for the internet).
library("plotGoogleMaps") lat <- c(-38.31, -35.50) #define our map's ylim lon <- c(40.96,37.50) #define our map's xlim # make your coordinates a data frame coords <- as.data.frame(cbind(lon=lon,lat=lat)) # make it a spatial object by defining its coordinates in a reference system coordinates(coords) <- ~lat+lon # you also need a reference system, the following should be a fine default proj4string(coords) <- CRS("+init=epsg:4326") # Note: it is a short for: CRS("+init=epsg:4326") > CRS arguments: > +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 # then just plot a <- plotGoogleMaps(coords) # here `a <-` avoids that you get flooded by the html version of what you plot