自定义分级地图

huangapple go评论101阅读模式
英文:

Customizing Choropleth Map

问题

我正在尝试制作一个使用来自亚洲的多个国家的Choropleth地图。下面是代码和国家。

  1. library(ggplot2)
  2. library(maps)
  3. datar <- data.frame(GEO = c("AR","AZ","TR","GA","IR"),
  4. Value = c(0.2560,0.023,0.0120,0.158,0.0025),
  5. search_countries = factor(c("Armenia","Azerbaijan","Turkey","Georgia","Iran")),
  6. countries = c("Armenia","Azerbaijan","Turkey","Georgia","Iran")
  7. )
  8. mapa <- map_data('world', region = datar$search_countries)
  9. ggplot() +
  10. geom_map(data = mapa, map = mapa, aes(x = long, y = lat, map_id = region)) +
  11. geom_map(data = datar, map = mapa, aes(fill = Value, map_id = countries), color ='black', size = .25) +
  12. coord_map()

现在我想要自定义这个地图。具体来说,我想在地图上看到国家的名称,还想看到湖泊。

所以有人可以帮助我解决这个问题吗?

英文:

I am trying to make a Choropleth map with several countries from Asia. Below you can see the code and countries.

  1. library(ggplot2)
  2. library(maps)
  3. datar &lt;- data.frame(GEO = c(&quot;AR&quot;,&quot;AZ&quot;,&quot;TR&quot;,&quot;GA&quot;,&quot;IR&quot;),
  4. Value = c(0.2560,0.023,0.0120,0.158,0.0025),
  5. search_countries = factor(c(&quot;Armenia&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)),
  6. countries = c(&quot;Armenia&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)
  7. )
  8. mapa &lt;- map_data(&#39;world&#39;, region = datar$search_countries)
  9. ggplot() +
  10. geom_map(data = mapa, map = mapa, aes(x = long, y = lat,map_id = region)) +
  11. geom_map(data = datar, map = mapa, aes(fill = Value, map_id = countries), color =&#39;black&#39;, size = .25) +
  12. 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 包中获取高清地图。

  1. library(ggplot2)
  2. library(rnaturalearth)
  3. library(sf)
  4. url <- paste0("https://www.naturalearthdata.com/",
  5. "http//www.naturalearthdata.com/download/10m/physical/",
  6. "ne_10m_lakes.zip")
  7. path <- tempdir()
  8. download.file(url, paste0(path, "/lakes.zip"))
  9. unzip(paste0(path, "/lakes.zip"))
  10. lakes <- read_sf("ne_10m_lakes.shp")

我们可以将国家和湖泊的详细轮廓与您自己的数据结合起来,像这样:

  1. datar <- data.frame(GEO = c("AR", "AZ", "TR", "GA", "IR"),
  2. Value = c(0.2560, 0.023, 0.0120, 0.158, 0.0025),
  3. search_countries = factor(c("Armenia", "Azerbaijan",
  4. "Turkey", "Georgia", "Iran")),
  5. countries = c("Armenia", "Azerbaijan", "Turkey", "Georgia",
  6. "Iran"))
  7. datar$geometry <- ne_countries(scale = 10,
  8. country = datar$countries,
  9. returnclass = "sf")$geometry
  10. datar$centroid <- st_centroid(datar$geometry)

然后绘图代码非常简单:

  1. ggplot(datar) +
  2. geom_sf(aes(geometry = geometry, fill = Value)) +
  3. geom_sf(data = lakes, color = NA, fill = "white") +
  4. geom_sf_text(aes(geometry = centroid, label = countries)) +
  5. coord_sf(xlim = st_bbox(datar$geometry)[c(1, 3)],
  6. ylim = st_bbox(datar$geometry)[c(2, 4)]) +
  7. theme_classic(base_size = 20) +
  8. theme(panel.border = element_rect(fill = NA)) +
  9. 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.

  1. library(ggplot2)
  2. library(rnaturalearth)
  3. library(sf)
  4. url &lt;- paste0(&quot;https://www.naturalearthdata.com/&quot;,
  5. &quot;http//www.naturalearthdata.com/download/10m/physical/&quot;,
  6. &quot;ne_10m_lakes.zip&quot;)
  7. path &lt;- tempdir()
  8. download.file(url, paste0(path, &quot;/lakes.zip&quot;))
  9. unzip(paste0(path, &quot;/lakes.zip&quot;))
  10. lakes &lt;- read_sf(&quot;ne_10m_lakes.shp&quot;)

We can combine detailed outlines of countries and lakes to your own data like so:

  1. datar &lt;- data.frame(GEO = c(&quot;AR&quot;, &quot;AZ&quot;, &quot;TR&quot;, &quot;GA&quot;, &quot;IR&quot;),
  2. Value = c(0.2560, 0.023, 0.0120, 0.158, 0.0025),
  3. search_countries = factor(c(&quot;Armenia&quot;, &quot;Azerbaijan&quot;,
  4. &quot;Turkey&quot;, &quot;Georgia&quot;, &quot;Iran&quot;)),
  5. countries = c(&quot;Armenia&quot;, &quot;Azerbaijan&quot;, &quot;Turkey&quot;, &quot;Georgia&quot;,
  6. &quot;Iran&quot;))
  7. datar$geometry &lt;- ne_countries(scale = 10,
  8. country = datar$countries,
  9. returnclass = &quot;sf&quot;)$geometry
  10. datar$centroid &lt;- st_centroid(datar$geometry)

Then the plotting code is pretty straightforward

  1. ggplot(datar) +
  2. geom_sf(aes(geometry = geometry, fill = Value)) +
  3. geom_sf(data = lakes, color = NA, fill = &quot;white&quot;) +
  4. geom_sf_text(aes(geometry = centroid, label = countries)) +
  5. coord_sf(xlim = st_bbox(datar$geometry)[c(1, 3)],
  6. ylim = st_bbox(datar$geometry)[c(2, 4)]) +
  7. theme_classic(base_size = 20) +
  8. theme(panel.border = element_rect(fill = NA)) +
  9. scale_fill_gradient(low = &quot;#d0d8c8&quot;, high = &quot;#ffd8d0&quot;)

自定义分级地图

huangapple
  • 本文由 发表于 2023年1月9日 03:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定