r的访问国家世界地图
像我一样,如果你喜欢像R一样旅行,你可能想要绘制你在R的国家的世界地图。下面是我在2020年1月20日访问的国家的一个例子:
要在R中绘制此地图,您需要以下包:
library(highcharter)
library(dplyr)
library(maps)
As usual, you need the packages to be installed on your machine before loading them with library()
. You can install a package with the command install.packages("name_of_package")
.
After having loaded the packages, we are going to use the dataset called iso3166
from the {maps}
package and rename it dat
. Here are the first 6 rows of the dataset:
dat <- iso3166
head(dat)
## a2 a3 ISOname mapname sovereignty
## 1 AW ABW Aruba Aruba Netherlands
## 2 AF AFG Afghanistan Afghanistan Afghanistan
## 3 AO AGO Angola Angola Angola
## 4 AI AIA Anguilla Anguilla Anguilla
## 5 AX ALA Aland Islands Finland:Aland Islands Finland
## 6 AL ALB Albania Albania Albania
We rename the variable a3
by iso-a3
:
dat <- rename(dat, "iso-a3" = a3)
head(dat)
## a2 iso-a3 ISOname mapname sovereignty
## 1 AW ABW Aruba Aruba Netherlands
## 2 AF AFG Afghanistan Afghanistan Afghanistan
## 3 AO AGO Angola Angola Angola
## 4 AI AIA Anguilla Anguilla Anguilla
## 5 AX ALA Aland Islands Finland:Aland Islands Finland
## 6 AL ALB Albania Albania Albania
We save the visited countries in a vector called countries_visited
. To know the ISO codes of the countries you have visited, check the column ISOname
in the dataset and extract the ISO codes from the column iso-a3
:
countries_visited <- c("AUS", "BEL", "CAN", "CZE", "DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "IRL", "ITA", "LVA", "LUX", "MCO", "MMR", "NLD", "NZL", "NOR", "PRT", "ROU", "SGP", "ESP", "SWE", "CHE", "TWN", "THA", "GBR", "USA")
We now create a new variable called visited
which equals to 1 if you have visited the country and 0 otherwise:
dat$visited <- ifelse(dat$`iso-a3` %in% countries_visited, 1, 0)
head(dat)
## a2 iso-a3 ISOname mapname sovereignty visited
## 1 AW ABW Aruba Aruba Netherlands 0
## 2 AF AFG Afghanistan Afghanistan Afghanistan 0
## 3 AO AGO Angola Angola Angola 0
## 4 AI AIA Anguilla Anguilla Anguilla 0
## 5 AX ALA Aland Islands Finland:Aland Islands Finland 0
## 6 AL ALB Albania Albania Albania 0
Finally, we are ready to draw the world map thanks to the hcmap()
command from the {highcharter}
package:
hcmap(
map = "custom/world-highres3", # high resolution world map
data = dat, # name of dataset
joinBy = "iso-a3",
value = "visited",
showInLegend = FALSE, # hide legend
nullColor = "#DADADA",
download_map_data = TRUE
) %>%
hc_mapNavigation(enabled = FALSE) %>%
hc_legend("none") %>%
hc_title(text = "World map") # title
将参数更改为您的需求,您很高兴。要进一步进一步,您还可以添加一个列表,包括所有访问国家的代码:
dat <- subset(dat, dat$visited == 1)
sort(dat$ISOname) # sort is to have the visited countries in alphabetical order
## [1] "Australia"
## [2] "Belgium"
## [3] "Canada"
## [4] "Clipperton Island"
## [5] "Czech Republic"
## [6] "Denmark"
## [7] "Finland"
## [8] "France"
## [9] "Germany"
## [10] "Greece"
## [11] "Hungary"
## [12] "Ireland"
## [13] "Italy"
## [14] "Latvia"
## [15] "Luxembourg"
## [16] "Monaco"
## [17] "Myanmar"
## [18] "Netherlands"
## [19] "New Zealand"
## [20] "Norway"
## [21] "Portugal"
## [22] "Portugal"
## [23] "Portugal"
## [24] "Romania"
## [25] "Singapore"
## [26] "Spain"
## [27] "Spain"
## [28] "Sweden"
## [29] "Switzerland"
## [30] "Taiwan"
## [31] "Thailand"
## [32] "United Kingdom of Great Britain and Northern Ireland"
## [33] "United States"
并计算访问的国家数量:
paste(
"Total: ",
sum(dat$visited),
" countries."
)
## [1] "Total: 33 countries."
总之,这里是突出显示的访问国家的世界地图的整个代码,所有国家的列表按字母顺序排列和访问的国家数量:
library(highcharter)
library(dplyr)
library(maps)
dat <- iso3166
dat <- rename(dat, "iso-a3" = a3)
countries_visited <- c("AUS", "BEL", "CAN", "CZE", "DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "IRL", "ITA", "LVA", "LUX", "MCO", "MMR", "NLD", "NZL", "NOR", "PRT", "ROU", "SGP", "ESP", "SWE", "CHE", "TWN", "THA", "GBR", "USA")
dat$visited <- ifelse(dat$`iso-a3` %in% countries_visited, 1, 0)
hcmap(
map = "custom/world-highres3", # high resolution world map
data = dat, # name of dataset
joinBy = "iso-a3",
value = "visited",
showInLegend = FALSE, # hide legend
nullColor = "#DADADA",
download_map_data = TRUE
) %>%
hc_mapNavigation(enabled = FALSE) %>%
hc_legend("none") %>%
hc_title(text = "World map") # title
dat <- subset(dat, dat$visited == 1)
sort(dat$ISOname) # sort is to have the visited countries in alphabetical order
paste(
"Total: ",
sum(dat$visited),
" countries."
)
谢谢阅读。我希望这篇文章帮助您使用R.突出显示的访问国家的世界地图。
一如既往,如果您有问题或与本文所涵盖的主题相关的建议,请将其添加为评论,以便其他读者可以从讨论中受益。
喜欢这篇文章?
获取更新 每次发布新文章。任何垃圾邮件都没有任何垃圾邮件。