r - 使用 ggplot2 在世界地图中绘制子区域

标签 r maps

我正在尝试从名为 data 的数据框中绘制这些国家/地区:

               country value        lon        lat
1              Denmark    12   9.501785  56.263920
2     UK:Great Britain    13  -1.174320  52.355518
3               France    15   2.213749  46.227638
4              Germany    17  10.451526  51.165691
5      China:Hong Kong    18 114.174695  22.278315
6          Netherlands    31   5.291266  52.132633
7          New Zealand    32 174.885971 -40.900557
8  UK:Northern Ireland    33  -6.492314  54.787715
9               Norway    34   8.468946  60.472024
10        Saudi Arabia    40  45.079162  23.885942
11              Serbia    41  21.005859  44.016521
12           Singapore    42 103.819836   1.352083
13     Slovak Republic    43 101.724578   3.153870
14            Slovenia    44  14.995463  46.151241
15        South Africa    45  22.937506 -30.559482

我正在使用 worldmap 和 ggplot 库:

library(maps)       # Provides functions that let us plot the maps
library(ggplot2)    # Generic graphis engine

map = map_data("world")
map = subset(map, region!="Antarctica") #Remove Antarctica from map

Countries = ggplot() + 
  geom_polygon(data = map, aes(x=long, y = lat, group = group), fill = NA, colour="darkgray", size=0.5)+
  geom_map(data=data,map=map,aes(map_id=country, x=lon, y=lat),fill = "cornflowerblue", colour = "gray") +
  coord_equal()
Countries

我可以绘制除 UK:Great BritainChina: Hong Kong 以外的所有国家,实际上所有其他地区和次区域都用“:”分隔:

Country map

我对如何使用 world_map 和 ggplot 绘制 UK:Great Britain 的想法用尽了。大家有遇到过类似的问题,或者能想到解决办法吗?提前致谢。

最佳答案

这实现了您正在寻找的结果,但我不确定它是否是一个非常有用的 map

library(ggplot2)
library(data.table)
library(magrittr)

map_dat <- subset(map_data("world"), region!="Antarctica")
setDT(map_dat)

# the countries you need to translate to region:subregion
colon_countries <-
  grep(':', data$country, value=T) %>%
    sub(':.*$', '', .) %>%
    unique

# change region to region:subregion, 
# for countries of interest, for rows with a value of subregion
map_dat[region %in% colon_countries, 
        region := ifelse(!is.na(subregion),
                         paste0(region, ':', subregion),
                         region)]
ggplot() + 
  geom_polygon(data = map_dat,
               aes(x=long, y = lat, group = group),
               fill = NA, colour="darkgray", size=0.5)+
  geom_map(data = data, map = map_dat,
           aes(map_id = country),
           fill = "cornflowerblue", colour = 'gray') +
  # probably not the projection you really want, but leaving it to match your post above
  coord_equal()

enter image description here

关于r - 使用 ggplot2 在世界地图中绘制子区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39278340/

相关文章:

r - 在 R 中的 arima.sim 中初始化

c++ - 通过迭代打印 C++ Map

javascript - 谷歌地图将边界限制在世界的一个实例

r - 如何设置 R 从没有 bin 或 root 访问权限的目录运行?

r - 在ggplot2中使用圆形包装可视化分层数据?

android - 检查用户是否不遵循 Android 谷歌地图中的路线

java - 谷歌地图 - 我正在尝试更新当前位置,但我的应用程序一打开就关闭了。这是代码-

javascript - 我将如何在我的 postgres 表中为使用 html5 map 的用户保存用户的纬度和经度?

R 的 lm() 函数删除因子级别(除了截距估计器)而不报告错误或警告

r - 更改 x 轴刻度标签 ggplot2 不起作用,使轴消失,scale_x_discrete