自定义分级地图

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

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 &lt;- data.frame(GEO = c(&quot;AR&quot;,&quot;AZ&quot;,&quot;TR&quot;,&quot;GA&quot;,&quot;IR&quot;), 
                      Value = c(0.2560,0.023,0.0120,0.158,0.0025),
                      search_countries = factor(c(&quot;Armenia&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)),
                      countries = c(&quot;Armenia&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)
  )
  
  mapa &lt;- map_data(&#39;world&#39;, 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 =&#39;black&#39;, 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 &lt;- paste0(&quot;https://www.naturalearthdata.com/&quot;,
              &quot;http//www.naturalearthdata.com/download/10m/physical/&quot;,
              &quot;ne_10m_lakes.zip&quot;)

path &lt;- tempdir()
download.file(url, paste0(path, &quot;/lakes.zip&quot;))
unzip(paste0(path, &quot;/lakes.zip&quot;))
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:

datar &lt;- data.frame(GEO = c(&quot;AR&quot;, &quot;AZ&quot;, &quot;TR&quot;, &quot;GA&quot;, &quot;IR&quot;), 
                    Value = c(0.2560, 0.023, 0.0120, 0.158, 0.0025),
                    search_countries = factor(c(&quot;Armenia&quot;, &quot;Azerbaijan&quot;,
                                                &quot;Turkey&quot;, &quot;Georgia&quot;, &quot;Iran&quot;)),
                    countries = c(&quot;Armenia&quot;, &quot;Azerbaijan&quot;, &quot;Turkey&quot;, &quot;Georgia&quot;,
                                  &quot;Iran&quot;))

datar$geometry &lt;- ne_countries(scale = 10, 
                               country = datar$countries, 
                               returnclass = &quot;sf&quot;)$geometry

datar$centroid &lt;- 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 = &quot;white&quot;) +
  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 = &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:

确定