Bạn hãy trích xuất ra dữ liệu của hai năm 2015 và 2026 để vẽ bản đồ sau.
Gợi ý thực hiện:
Bạn có thể sử dụng dataset worldmap để vẽ bản đồ nền bằng package ggplot2
library(ggplot2)world <- ggplot2:::map_data("world")set.seed(1)ggplot() +geom_polygon(data = world,aes(x = long,y = lat, group = group,fill =as.factor(group))) +coord_equal() +theme(legend.position ="none")
Một số tên quốc gia trong dataset total_fertility_rate_2015_2026.xlsx sẽ khác với trong dataset world bạn cần chuẩn lại cùng tên để có thể vẽ được bản đồ heatmap theo tỷ lệ sinh.
df$Country[which(df$Country =="United Kingdom")] <-"UK"df$Country[which(df$Country =="Republic of Moldova")] <-"Moldova"df$Country[which(df$Country =="Bonaire, Sint Eustatius and Saba")] <-"Bonaire"df$Country[which(df$Country =="Congo, Democratic Republic of the")] <-"Democratic Republic of the Congo"df$Country[which(df$Country =="Congo")] <-"Republic of Congo"df$Country[which(df$Country =="Curaçao")] <-"Curacao"df$Country[which(df$Country =="British Virgin Islands" )] <-"Virgin Islands"df$Country[which(df$Country =="State of Palestine" )] <-"Palestine"df$Country[which(df$Country =="Cabo Verde" )] <-"Cape Verde"df$Country[which(df$Country =="Syrian Arab Republic" )] <-"Syria"df$Country[which(df$Country =="Trinidad and Tobago" )] <-"Tobago"df$Country[which(df$Country =="Côte d'Ivoire" )] <-"Ivory Coast"
Setdiff
Kiểm tra xem tên quốc gia ở hai dataset có khác biệt không để clean lại lần nữa.
unique(world$region) -> country_mapunique(df$Country) -> country_tfrsetdiff(country_tfr, country_map) # ten quoc gia co trong tfr nhung ko co trong country_map
[1] "China, Hong Kong SAR" "China, Macao SAR" "Eswatini" "Falkland Islands (Malvinas)" "Gibraltar"
[6] "Micronesia (Fed. States of)" "Réunion" "Saint Barthélemy" "Saint Kitts and Nevis" "Saint Martin (French part)"
[11] "Saint Vincent and the Grenadines" "Sint Maarten (Dutch part)" "Tokelau" "Tuvalu" "United States Virgin Islands"
[16] "Wallis and Futuna Islands"
setdiff(country_map, country_tfr) # ten quoc gia co trong country_map nhung ko co trong tfr