Refs
OpenStreeMap http://download.geofabrik.de/
http://www.molecularecologist.com/2012/09/making-maps-with-r/
Draft version, still lots to explore
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
uk <- readShapeSpatial("map.shp", proj4string=CRS("+proj=longlat"))
plot(uk)
summary(uk)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
## min max
## x -13.69139 1.764168
## y 49.86542 60.845822
## Is projected: FALSE
## proj4string : [+proj=longlat +ellps=WGS84]
## Data attributes:
## MM_UID CODE NAME1 NAME2
## Min. : 1.00 GB.ED : 2 England :112 Aberdeen : 1
## 1st Qu.: 48.75 GB.AB : 1 Northern Ireland: 26 Aberdeenshire: 1
## Median : 96.50 GB.AD : 1 Scotland : 32 Anglesey : 1
## Mean : 96.50 GB.AG : 1 Wales : 22 Angus : 1
## 3rd Qu.:144.25 GB.AM : 1 Antrim : 1
## Max. :192.00 GB.AN : 1 Ards : 1
## (Other):185 (Other) :186
## DIVISION
## Kingdom :144
## Principality: 22
## Province : 26
##
##
##
##
library(maps)
france<-map(database="france")
names(france)
## [1] "x" "y" "range" "names"
head(france$names)
## [1] "Nord" "Pas-de-Calais" "Somme" "Nord:1"
## [5] "Ardennes" "Seine-Maritime"
library(mapdata)
map(database="worldHires", "Portugal")
library(RgoogleMaps)
lat <- c(37,42) #define our map's ylim
lon <- c(-9,-6) #define our map's xlim
center = c(mean(lat), mean(lon)) #tell what point to center on
zoom <- 7 #zoom: 1 = furthest out (entire globe), larger numbers = closer in
terrmap <- GetMap(center=center, zoom=zoom, maptype= "terrain", destfile = "terrain.png")
# lots of visual options, just like google maps:
# maptype = c("roadmap", "mobile", "satellite", "terrain", "hybrid", "mapmaker-roadmap", "mapmaker-hybrid")
PlotOnStaticMap(terrmap)
path = paste0("&path=color:0x0000ff|weight:3|",
"38.7138,-9.14|39.12,-9.21|39.0,-8.5")
MyMap <- GetMap(center=center, zoom=zoom, path = path)
PlotOnStaticMap(MyMap)
library(ggmap)
## Loading required package: ggplot2
library(geosphere) # use: gcIntermediate
# this example shows obtaining coordinates (latitude, longitude) using
# map_world function, and drawing them using geom_polygon
world <- map_data("world")
basemap <- ggplot(legend = FALSE) +
geom_polygon(data = world, aes(x = long, y = lat, group = group, fill = group)) +
theme(legend.position = "none")
lx <- geocode("Lisbon, PT") # find coordinates
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lisbon,+PT&sensor=false
ny <- geocode("New York, USA")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=New+York,+USA&sensor=false
inter <- as.data.frame(gcIntermediate(c(lx$lon, lx$lat), c(ny$lon, ny$lat), n = 50, addStartEnd = TRUE))
basemap + geom_line(data = inter, aes(x = lon, y = lat), color = "red")
# using maps to plot routes
rt1 = route(from = "Lisbon", to = "Castelo Branco", mode = "driving")
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=Lisbon&destination=Castelo+Branco&mode=driving&units=metric&alternatives=false&sensor=false
PortugalMap <- qmap("Portugal", zoom = 8, color = "bw")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Portugal&zoom=8&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Portugal&sensor=false
PortugalMap + geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat),
color = "blue", data = rt1)
# select a map and plot some markers
LisbonMap = qmap("Lisboa", zoom = 16, color = "bw", maptype='hybrid', legend = "topleft",
location = c(lon = -9.138, lat = 38.7098))
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=38.7098,-9.138&zoom=16&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
LisbonMap
# make some data
df <- data.frame(lat=c(38.7098,38.7139,38.7122),
lon=c(-9.1330,-9.1336,-9.1400),
type=c("igreja", "castelo", "igreja"),
nome=c("sé lisboa", "castelo são jorge", "convento do carmo" ))
df
## lat lon type nome
## 1 38.7098 -9.1330 igreja sé lisboa
## 2 38.7139 -9.1336 castelo castelo são jorge
## 3 38.7122 -9.1400 igreja convento do carmo
LisbonMap + geom_point(aes(x = lon, y = lat, colour = type, shape=type), data = df)
library(maps)
library(mapproj)
countries <- c("Portugal", "Poland", "Greece", "Iran", "Brazil")
m <- map("world", plot = FALSE)
countryMatch <- match.map("world", countries)
# color code any states with visit data as light blue
colorCountry <- sapply(countryMatch, function(c) {
if (!is.na(c))
c <- "#FF0000" else c <- "#FFFFFF"
})
map("world", proj = "azequalarea", orient = c(25, 10, 0), boundary = TRUE, col = colorCountry, fill = TRUE)
map.grid(m, col = "lightblue", label = FALSE, lty = 2, pretty = TRUE)
map.scale()
Ref: http://blog.corynissen.com/2015/05/creating-styled-google-maps-in-ggmap.html
library("RJSONIO")
library("ggmap")
library("magrittr")
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:ggmap':
##
## inset
# You can define a style here:
# http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html
style <- '[
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{ "visibility": "on" },
{ "gamma": 0.61 },
{ "color": "#284180" },
{ "hue": "#00ff44" },
{ "saturation": 46 }
]
}
]'
style_list <- fromJSON(style, asText=TRUE)
create_style_string<- function(style_list){
style_string <- ""
for(i in 1:length(style_list)){
if("featureType" %in% names(style_list[[i]])){
style_string <- paste0(style_string, "feature:",
style_list[[i]]$featureType, "|")
}
elements <- style_list[[i]]$stylers
a <- lapply(elements, function(x)paste0(names(x), ":", x)) %>%
unlist() %>%
paste0(collapse="|")
style_string <- paste0(style_string, a)
if(i < length(style_list)){
style_string <- paste0(style_string, "&style=")
}
}
# google wants 0xff0000 not #ff0000
style_string <- gsub("#", "0x", style_string)
return(style_string)
}
style_string <- create_style_string(style_list)
ggmap(get_googlemap("lisbon", size=c(800,800), style=style_string, zoom=14), extent="device")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=lisbon&zoom=14&size=800x800&maptype=terrain&style=feature:poi.park%7Cvisibility:on%7Cgamma:0.61%7Ccolor:0x284180%7Chue:0x00ff44%7Csaturation:46&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=lisbon&sensor=false