Problematic World Map

我正试图在R Studio中创建一张世界 map ,其中我拥有数据的每个国家(不是每个国家)都以相对于该国家数据的渐变比例填充一种 colored颜色 --在本例中是进行Covid测试的数量--或‘Total_test’.我使用的数据是来自给我的一个示例数据集的Covid数据.问题是, map 就像上面一样--到处都是对角线.

到目前为止,我拥有的R码是;

install.packages("maps")
library(maps)

map_data_subset <- subset(covid, select = c("Country", "Continent","Total_tests", "Total_Deaths", "Total_Deaths_Per_Million"))
country_tests_deaths <- na.omit(map_data_subset)
world_map <- map_data("world")
merged_data <- merge(world_map, country_tests_deaths, by.x = "region", by.y = "Country", all.x = TRUE)


# Plotting map
ggplot(data = merged_data, aes(x = long, y = lat, group = group, fill = Total_tests)) +
  geom_polygon(color = "white") +
  scale_fill_gradient(low = "skyblue", high = "navyblue", name = "Your Legend") +
  theme_minimal()

我的问题是,我的世界 map 出来时看起来就像上面的那样--有人能告诉我为什么会这样吗?我怎么才能修复它呢?我有一种预感,这可能与世界 map 数据有大约100,000行有关,而我的测试数据只有大约100行,但我不确定这是不是真的,也不确定如何修复它.

编辑-下面是我想要叠加到世界 map 上的POST‘na.omit’数据集;

'''
> print(map_data_subset, n=104)
# A tibble: 104 × 2
    Country                          Total_tests
    <chr>                                  <dbl>
  1 Andorra                               215733
  2 Argentina                           24156096
  3 Armenia                              1770914
  4 Australia                           39943525
  5 Austria                             90377770
  6 Azerbaijan                           4952737
  7 Bahamas                               147633
  8 Bahrain                              6612314
  9 Bangladesh                           9907321
 10 Belgium                             20514883
 11 Belize                                278894
 12 Bhutan                               1160858
 13 Bolivia                              2456130
 14 Bosnia and Herzegovina               1253492
 15 Bulgaria                             4990220
 16 Canada                              44186056
 17 Chile                               22194480
 18 Colombia                            25954762
 19 Costa Rica                           1927328
 20 Cote d'Ivoire                        1029903
 21 Croatia                              2913176
 22 Cyprus                              13835984
 23 Denmark                             41501675
 24 Ecuador                              1761061
 25 Equatorial Guinea                     230491
 26 Estonia                              1997046
 27 Ethiopia                             3557710
 28 Finland                              7224917
 29 Gabon                                1188832
 30 Georgia                              8900000
 31 Germany                             75158696
 32 Greece                              21991126
 33 Guatemala                            2228737
 34 Hong Kong                           25906063
 35 Hungary                              6620866
 36 Iceland                               650933
 37 India                              583631490
 38 Iran                                33350660
 39 Iraq                                15338457
 40 Ireland                              7668724
 41 Israel                              28327710
 42 Italy                               95331171
 43 Jamaica                               617665
 44 Japan                               24344819
 45 Jordan                              10237614
 46 Kosovo                               1217376
 47 Kuwait                               4389988
 48 Laos                                  612410
 49 Liechtenstein                          69730
 50 Lithuania                            5381950
 51 Luxembourg                           3543784
 52 Maldives                             1543706
 53 Malta                                1306982
 54 Mexico                              10389202
 55 Moldova                              1822979
 56 Mongolia                             4074039
 57 Morocco                              8853903
 58 Mozambique                            901901
 59 Myanmar                              4376017
 60 Namibia                               713463
 61 Nepal                                4256803
 62 Netherlands                         13409992
 63 New Zealand                          3602589
 64 Nigeria                              3142971
 65 Norway                               7864561
 66 Pakistan                            19911021
 67 Panama                               3906709
 68 Paraguay                             1846953
 69 Philippines                         20507811
 70 Poland                              20707050
 71 Portugal                            19023656
 72 Qatar                                2723342
 73 Romania                             13294804
 74 Russia                             195638185
 75 Rwanda                               2938657
 76 Saint Kitts and Nevis                  46380
 77 Saint Vincent and the Grenadines       77176
 78 Saudi Arabia                        29430910
 79 Senegal                               828629
 80 Serbia                               5874429
 81 Singapore                           20436387
 82 Slovakia                            42271380
 83 Slovenia                             1626242
 84 South Africa                        17864698
 85 South Korea                         13721674
 86 Spain                               59314343
 87 Sri Lanka                            5355028
 88 Switzerland                         10760244
 89 Taiwan                               3619373
 90 Thailand                            14201188
 91 Timor                                 209126
 92 Togo                                  518580
 93 Trinidad and Tobago                   353373
 94 Tunisia                              2994047
 95 Turkey                              90162700
 96 Uganda                               1705808
 97 Ukraine                             13277259
 98 United Arab Emirates                87246490
 99 United Kingdom                     283376305
100 United States                      592381867
101 Uruguay                              3635691
102 Vietnam                             24871501
103 Zambia                               2509600
104 Zimbabwe                             1288436 
'''

先谢谢你,弗雷迪

推荐答案

100

以下是对空间数据使用sf包的解决方案.该问题是由您 Select 的世界数据引起的;map_data("world")返回点数据对象,该对象不保留每个点的关联顺序.我还没有研究R如何连接点的默认行为,但如果您好奇的话,可以研究一下.

考虑到这一点,使用sf包,该方法将WORLD_MAP对象转换为多边形SF对象.您仍然需要仔细判断联接字段中的值是否彼此对应.这是因为在某些情况下,一个国家可能会有不止一个名字,例如其他拼写.在本例中,有一些不匹配的记录因此(请参见下面的代码).这是依赖文本字段的联接的常见问题.您需要手动编辑这些名称,以确保您的所有数据都连接到WORLD_MAP.示例 map 未考虑不匹配的国家/地区:

library(maps)
library(ggplot2)
library(sf)
options(scipen = 999)

world_map <- map("world", exact = FALSE, plot = FALSE, fill = TRUE)
world_map <- world_map %>%
  st_as_sf() %>%
  rename(Country = "ID")

# Your updated data, I created a .csv then imported it to R
map_data_subset <- read.csv("C:/test/covid.csv", stringsAsFactors = FALSE)

# Strip trailing white spaces from Country column
map_data_subset$Country <- trimws(map_data_subset$Country)

# Join data
merged_data <- merge(world_map, 
                     map_data_subset,
                     by = "Country",
                     all.x = TRUE)

# As per my comment above, some country names have different spellings so you 
# will need to manually sort these out (on the joys of joins using text fields).
# These are the non-matched countries:
map_data_subset[!map_data_subset$Country %in% world_map$Country,]

#                              Country Total_tests
# 20                     Cote d'Ivoire     1029903
# 34                         Hong Kong    25906063
# 76             Saint Kitts and Nevis       46380
# 77  Saint Vincent and the Grenadines       77176
# 91                             Timor      209126
# 93               Trinidad and Tobago      353373
# 99                    United Kingdom   283376305
# 100                    United States   592381867

ggplot() +
  geom_sf(data = merged_data, # Note you can pass sf object directly to ggplot()
          aes(fill = Total_tests),
          colour = "white") +
  scale_fill_gradient(low = "skyblue",
                      high = "navyblue",
                      name = "Your Legend") +
  theme_minimal()

result

This is the generalised answer that was posted originally:

library(ggplot2)
library(rnaturalearth) # For world map data
library(sf) # For geospatial data
library(dplyr)
library(lubridate) # For date data

# World map as an sf object
countries <- ne_countries(returnclass = 'sf') %>% 
  rename(country = "name_long") # Rename to make join easier

# Covid data
covid <- read.csv("https://covid.ourworldindata.org/data/owid-covid-data.csv",
                       stringsAsFactors = FALSE)

map_data_subset <- na.omit(covid)

# Subset columns and get most recent data per country. Depending on your data, some of this
# may not be relevant
map_data_subset <- covid %>%
  select(location, continent, date, total_tests, total_deaths, total_deaths_per_million) %>%
  na.omit() %>%
  rename(country = "location") %>% # As before, rename to make join easier
  group_by(country) %>%
  mutate(date = ymd(date)) %>% # to enable sorting of date column is correct
  arrange(date) %>% # to ensure last value is most recent data
  slice(n()) %>% # Return only the most recent data per country
  ungroup()

# Join data to world map
merged_data <- countries %>%
  left_join(map_data_subset, by = "country")

ggplot() +
  geom_sf(data = merged_data, # Note you can pass sf object directly to ggplot()
          aes(fill = total_tests),
          colour = "white") +
  scale_fill_gradient(low = "skyblue",
                      high = "navyblue",
                      name = "Your Legend") +
  theme_minimal()

result

R相关问答推荐

基于现有类创建类的打印方法(即,打印tibles更长时间)

根据模式将一列拆分为多列,并在R中进行拆分

在R中使用Scale_y_Break后更改y轴标签

如何对2个列表元素的所有组合进行操作?

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

如何将R中数据帧中的任何Nas替换为最后4个值

您是否可以使用facet_rap设置一个较低的限制来对ggmap上的比例中断进行zoom ?

如何在R中平滑地绘制线图(不拟合)?

R:用GGPLATE,如何在两个独立的变量中制作不同形状的散点图?

使用RSelenium在R中抓取Reddit时捕获多个标签

函数可以跨多个列搜索多个字符串并创建二进制输出变量

KM估计的差异:SvyKm与带权重的调查

扩展R中包含列表的数据框

如何在PrePlot()中将多个元素设置为斜体

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

在使用SliderInput In Shiny(R)设置输入数据的子集时,保留一些情节痕迹

如何使用ggplot2根据绘图中生成的斜率对小平面进行排序?

如何使用循环从R中的聚合函数创建列,而不会在名称中给出&q;$&q;?

conditionPanel不考虑以下条件

在R中,如果一个值在同一数据帧中的任何特定列中,如何计算?