English 中文(简体)
I m 试图在“国家彩色”的“世界地图”中绘制一个世界地图,相对数字变量而言
原标题:I m trying to produce a world map in R Studio with country colour fill relative to a numeric variable
  • 时间:2023-12-26 16:14:32
  •  标签:
  • r
  • rworldmap

问题世界地图

我正试图在《世界地图集》中绘制一份世界地图,其中我拥有数据的每个国家(并非每个国家)都以与该国数据相比的渐进式颜色填满——在这种情况下,所管理的方格检测数量——或总数——测试。 使用Im的数据是从一个实例数据集一中收集的。 图卢布是上述地图,所有地方都有这些分界线。

我迄今为止的《区域法典》是;

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()

我的麻烦是,我的世界地图像上面那样看着——谁能告诉我,为什么,以及我如何确定? 我有一个小nch子,可能是与世界地图数据做的,该数据有十万row,而我的测试数据只有100.,但不能确定这种数据是否真实,也难以确定。

EDIT - 下面是N.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 
   

提前感谢, Freddy

问题回答

You haven t provided a minimal, reproducible example, so it sugh to knowledge what is caused yoru mis. 然而,这里的解决办法是使用数据-数据-数据-数据-数据-数据-数据检索数据,以及利用<代码>r Naturalearth包获得的世界地图。 这种方法使用空间数据<代码>sf。 海事组织,sf,是处理空间数据操纵和绘图的更有力方式。 你们还需要双重检查,共同领域的价值观相互对应。 这是因为,在某些情况下,一个国家可能有一个以上的名称,例如替代拼写。 还指出,在许多情况下,中国的数据没有按每周一次记录,因此,我可以为这些数据的准确性 t。 然而,贵方的数据可能没有这一问题:

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





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签