英文:
Customizing Choropleth Map
问题
我正在尝试制作一个使用来自亚洲的多个国家的Choropleth地图。下面是代码和国家。
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()
现在我想要自定义这个地图。具体来说,我想在地图上看到国家的名称,还想看到湖泊。
所以有人可以帮助我解决这个问题吗?
英文:
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?
答案1
得分: 3
需要获取更详细的地图,包括特定的湖泊地图。您可以从 rnaturalearth
包中获取高清地图。
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")
我们可以将国家和湖泊的详细轮廓与您自己的数据结合起来,像这样:
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)
然后绘图代码非常简单:
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")
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论