Skip to content

Instantly share code, notes, and snippets.

@academikuser
Created March 7, 2017 08:11
Show Gist options
  • Save academikuser/57cdcd6732b22be836d7f939b069692d to your computer and use it in GitHub Desktop.
Save academikuser/57cdcd6732b22be836d7f939b069692d to your computer and use it in GitHub Desktop.
R Tutorial: Mapping Israel with ggmap and a GIS shapefile
# load up area shape file:
```{r}
library(maptools)
area <- readShapePoly("ne_10m_parks_and_protected_lands_area.shp")
# # or file.choose:
# area <- readShapePoly(file.choose())
library(RColorBrewer)
colors <- brewer.pal(9, "BuGn")
library(ggmap)
mapImage <- get_map(location = c(lon = -118, lat = 37.5),
color = "color",
source = "osm",
# maptype = "terrain",
zoom = 6)
area.points <- fortify(area)
ggmap(mapImage) +
geom_polygon(aes(x = long,
y = lat,
group = group),
data = area.points,
color = colors[9],
fill = colors[6],
alpha = 0.5) +
labs(x = "Longitude",
y = "Latitude")
mapImageData2 <- get_map(location = c(lon = -0.016179, lat = 51.538525),
color = "color",
source = "google",
maptype = "satellite",
zoom = 15)
ggmap(mapImageData2,
extent = "device",
ylab = "Latitude",
xlab = "Longitude")
```
I want to use this google map
lat, lon
https://www.google.co.il/maps/@31.3222595,35.1660235,8z?hl=en
```{r}
## map of Brit Mandate + Sinai
mapImageData3 <- get_map(location = c(lon = 35.1660235,
lat = 31.32226),
color="color",
source = "google",
maptype = "satellite",
zoom = 7) ## 3 is spain to India big
## 16 is the Olympic stadium
## 6 is Suez to Euphrates
## 7 works, but too wide
## 8 cuts off part of Golan
## read the shape file ;; https://www.r-bloggers.com/shapefiles-in-r/
IsraelPolygon <- readShapePoly("/Users/AbuDavid/Downloads/ISR_adm/ISR_adm1.shp")
## make it graphable
IsraelPoints <-fortify(IsraelPolygon)
## pick colors for areas
library(RColorBrewer)
colors <- brewer.pal(9, "BuGn")
# # or file.choose:
# area <- readShapePoly(file.choose())
ggmap(mapImageData3,
extent = "device",
ylab = "Latitude",
xlab = "Longitude") +
geom_polygon(aes(x=long,
y=lat,
group=group),
data=IsraelPoints,
color=colors[9],
fil= colors[6],
alpha=0.5) +
labs(x="Longitude",
y="Latitude")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment