I am trying to make a Choropleth map with several countries from Asia. Below you can see the code and countries.
library(ggplot2)
library(maps)
datar <- data.frame(GEO = c("AR","AZ","TR","GA","IR"),
Value = c(0.2560,0.023,0.0120,0.158,0.0025),
search_countries = factor(c("Armenia","Azerbaijan","Turkey","Georgia","Iran")),
countries = c("Armenia","Azerbaijan","Turkey","Georgia","Iran")
)
mapa <- map_data('world', region = datar$search_countries)
ggplot()
geom_map(data = mapa, map = mapa, aes(x = long, y = lat,map_id = region))
geom_map(data = datar, map = mapa, aes(fill = Value, map_id = countries), color ='black', size = .25)
coord_map()
Now I want to customize this map. Namely, on the map I want to see the names of the countries and also I want to see lakes.
So can anybody help me how to solve this problem?
CodePudding user response:
You will need to get a hold of more detailed maps, including specific maps of lakes. You can get high definition maps from the rnaturalearth
package.
library(ggplot2)
library(rnaturalearth)
library(sf)
url <- paste0("https://www.naturalearthdata.com/",
"http//www.naturalearthdata.com/download/10m/physical/",
"ne_10m_lakes.zip")
path <- tempdir()
download.file(url, paste0(path, "/lakes.zip"))
unzip(paste0(path, "/lakes.zip"))
lakes <- read_sf("ne_10m_lakes.shp")
We can combine detailed outlines of countries and lakes to your own data like so:
datar <- data.frame(GEO = c("AR", "AZ", "TR", "GA", "IR"),
Value = c(0.2560, 0.023, 0.0120, 0.158, 0.0025),
search_countries = factor(c("Armenia", "Azerbaijan",
"Turkey", "Georgia", "Iran")),
countries = c("Armenia", "Azerbaijan", "Turkey", "Georgia",
"Iran"))
datar$geometry <- ne_countries(scale = 10,
country = datar$countries,
returnclass = "sf")$geometry
datar$centroid <- st_centroid(datar$geometry)
Then the plotting code is pretty straightforward
ggplot(datar)
geom_sf(aes(geometry = geometry, fill = Value))
geom_sf(data = lakes, color = NA, fill = "white")
geom_sf_text(aes(geometry = centroid, label = countries))
coord_sf(xlim = st_bbox(datar$geometry)[c(1, 3)],
ylim = st_bbox(datar$geometry)[c(2, 4)])
theme_classic(base_size = 20)
theme(panel.border = element_rect(fill = NA))
scale_fill_gradient(low = "#d0d8c8", high = "#ffd8d0")