I use the rworldmap
package with WorldBank Data and I enjoy it. I want to plot maps for Iran, with data related to each province. What are the steps to do that? I know we can plot maps like that in R
for some countries like US but not for all countries.
You can combine rworldmap
with the great suggestion from @jazzurro of using raster
to get GADM boundaries.
I suspect your main difficulty might be getting the province names to match between your data and the map.
The example below, uses defaults that you can change and just gives a different colour for each province.
library(raster)
library(rworldmap)
## 1 Get map of provinces (level 1 admin regions)
iranLevel1<- raster::getData("GADM", country = "Iran", level = 1)
## 2 join your [data] onto the map by specifying the join column in each
## this will report on any mis-matches between province names
#iranLevel1 <- rworldmap::joinData2Map([data],nameMap="iranLevel1",nameJoinIDMap="NAME_1",nameJoinColumnData=[insert])
## 3 plot map (change NAME_1 to the data you want to plot)
rworldmap::mapPolys(iranLevel1, nameColumnToPlot="NAME_1", addLegend=FALSE)
## 4 add text labels for provinces
text(iranLevel1, label="NAME_1", cex=0.7)
Note that joinData2Map()
, mapPolys()
are more generic equivalents of joinCountryData2Map()
, mapCountryData()
.
Another way of doing this would be to use the choroplethr
package.
You can directly import GADM data using the raster
package. Then, you can draw a map using ggplot2. When you download data, you can specify different level. Depending on this you see different boundaries.
library(raster)
library(ggplot2)
### Get data
iran<- getData("GADM", country = "Iran", level = 2)
### SPDF to DF
map <- fortify(iran)
### Draw a map
ggplot() +
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group))
EDIT
Seeing Andy's answer, I'd like to show how to add province names in ggplot2
. This is something I learned from @hrbrmstr. The rworldmap
package allows you to type less. The ggplot2
package still offers very nice graphics as well.
library(raster)
library(rgdal)
library(rgeos)
library(ggplot2)
library(dplyr)
### Get data
iran<- getData("GADM", country = "Iran", level = 1)
### SPDF to DF
map <- fortify(iran)
map$id <- as.integer(map$id)
dat <- data.frame(id = 1:(length(iran@data$NAME_1)), state = iran@data$NAME_1)
map_df <- inner_join(map, dat, by = "id")
# Find a center point for each province
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state
### This is hrbrmstr's own function
theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=rel(1.2)),
legend.title=element_text(size=rel(1.4), face="bold", hjust=0),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5),
strip.background=element_rect(fill="grey90", colour="grey50"),
strip.text.x=element_text(size=rel(0.8)),
strip.text.y=element_text(size=rel(0.8), angle=-90)
)
}
ggplot() +
geom_map(data = map_df, map = map_df,
aes(map_id = id, x = long, y = lat, group = group),
color = "#ffffff", fill = "#bbbbbb", size = 0.25) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 3) +
coord_map() +
labs(x = "", y = "", title = "Iran Province") +
theme_map()
You can combine rworldmap
with the great suggestion from @jazzurro of using raster
to get GADM boundaries.
I suspect your main difficulty might be getting the province names to match between your data and the map.
The example below, uses defaults that you can change and just gives a different colour for each province.
library(raster)
library(rworldmap)
## 1 Get map of provinces (level 1 admin regions)
iranLevel1<- raster::getData("GADM", country = "Iran", level = 1)
## 2 join your [data] onto the map by specifying the join column in each
## this will report on any mis-matches between province names
#iranLevel1 <- rworldmap::joinData2Map([data],nameMap="iranLevel1",nameJoinIDMap="NAME_1",nameJoinColumnData=[insert])
## 3 plot map (change NAME_1 to the data you want to plot)
rworldmap::mapPolys(iranLevel1, nameColumnToPlot="NAME_1", addLegend=FALSE)
## 4 add text labels for provinces
text(iranLevel1, label="NAME_1", cex=0.7)
Note that joinData2Map()
, mapPolys()
are more generic equivalents of joinCountryData2Map()
, mapCountryData()
.
Another way of doing this would be to use the choroplethr
package.
© 2022 - 2024 — McMap. All rights reserved.