Tiếp nối từ câu chuyện vẽ heatmap cho bản đồ thế giới theo sản lượng các loại nông sản ở link này

https://rpubs.com/tuhocr/huong-dan-ve-world-map

Hôm nay mình sẽ hướng dẫn các bạn các extract thông tin world ra region để vẽ về tình hình sản xuất lúa gạo ở khu vực Đông Nam Á nhé.

Import dataset

Load dataset vào R

library("googlesheets4")
options(scipen = 15, digits = 15, width = 200)

rice_production <- read_sheet('11EU8qJt14EflHgU2cCENQnIJUZKIClAMWHaFa_CEchk')

Clean dataset

Dataset khi import vào R có một số chỗ nho nhỏ cần làm sạch để gom vào dataset master trước khi dùng để vẽ đồ thị world map. Đơn vị quy chuẩn là metric ton (1000 kg).

### Cleaning dataset rice_production
rice_production_df <- as.data.frame(rice_production)
rice_production_2020 <- rice_production_df[, c(2, 3)] # chọn những cột quan tâm

### Loại NA
rice_production_2020 <- na.omit(rice_production_2020)

names(rice_production_2020) <- c("country", "rice_2020")

### In dataset
head(rice_production_2020, n = 10)
##        country rice_2020
## 2        China 211860000
## 3        India 178305000
## 4   Bangladesh  54905891
## 5    Indonesia  54649202
## 6      Vietnam  42758897
## 7     Thailand  30231025
## 8      Myanmar  25100000
## 9  Philippines  19294856
## 10      Brazil  11091011
## 11    Cambodia  10960000

Merge dataset

Sau khi đã có các dataset con, ta sẽ gộp các dataset này căn cứ theo cột country.

options("max.print" = 5000)

rice_production_2020 -> data_4

### In dataset master
dim(data_4) # có 113 quốc gia có thông tin sản xuất lúa gạo
## [1] 113   2
data_4
##                              country rice_2020
## 2                              China 211860000
## 3                              India 178305000
## 4                         Bangladesh  54905891
## 5                          Indonesia  54649202
## 6                            Vietnam  42758897
## 7                           Thailand  30231025
## 8                            Myanmar  25100000
## 9                        Philippines  19294856
## 10                            Brazil  11091011
## 11                          Cambodia  10960000
## 12                     United States  10322990
## 13                             Japan   9706250
## 14                          Pakistan   8419276
## 15                           Nigeria   8172000
## 16                             Nepal   5550878
## 17                         Sri Lanka   5120924
## 18                             Egypt   4893507
## 19                       South Korea   4713162
## 20                          Tanzania   4528000
## 21                        Madagascar   4232000
## 22                              Laos   3687336
## 23                              Peru   3436637
## 24                          Colombia   3424119
## 25                              Mali   3010027
## 26                            Guinea   2916395
## 27                          Malaysia   2321636
## 28                       North Korea   2113019
## 29                              Iran   2000000
## 30                            Taiwan   1750729
## 31                             Italy   1507490
## 32                       Ivory Coast   1481182
## 33  Democratic Republic of the Congo   1379000
## 34                           Senegal   1349723
## 35                           Ecuador   1336502
## 36                         Argentina   1222910
## 37                           Uruguay   1209000
## 38                          Paraguay   1187768
## 39                            Russia   1141819
## 40                      Sierra Leone   1049795
## 42                            Turkey    980000
## 43                             Ghana    973000
## 44                Dominican Republic    942765
## 45                             Spain    739230
## 46                            Guyana    687539
## 47                        Kazakhstan    556775
## 48                           Bolivia    487427
## 49                         Nicaragua    476992
## 50                              Iraq    464159
## 51                       Afghanistan    439549
## 52                         Venezuela    429179
## 53                             Benin    411578
## 54                      Burkina Faso    395443
## 55                            Panama    391600
## 56                        Mauritania    365000
## 57                          Cameroon    328503
## 58                            Mexico    295338
## 59                        Uzbekistan    293451
## 60                            Greece    287410
## 61                          Suriname    285712
## 62                              Chad    278053
## 63                           Liberia    270000
## 64                              Cuba    266595
## 65                            Uganda    200000
## 66                     Guinea-Bissau    198000
## 67                          Ethiopia    189649
## 68                             Kenya    180890
## 69                             Niger    179382
## 70                             Haiti    172000
## 71                             Chile    169697
## 72                              Togo    160000
## 73                           Burundi    150000
## 74                      Turkmenistan    150000
## 75                            Malawi    145446
## 76                        Costa Rica    137504
## 77                        Mozambique    137243
## 78                          Portugal    132790
## 79                            Rwanda    116504
## 81                        Tajikistan     99000
## 82                            France     76320
## 83                       Timor-Leste     69000
## 84                          Bulgaria     65810
## 85                          Honduras     65634
## 86                           Ukraine     60680
## 87                            Bhutan     54088
## 88                         Australia     50226
## 89                           Morocco     46275
## 90                        Kyrgyzstan     44474
## 91                            Zambia     34630
## 92                             Sudan     34133
## 93                         Guatemala     32000
## 94                           Comoros     30543
## 95                       El Salvador     28000
## 96                            Gambia     28000
## 97                           Romania     24670
## 98                   North Macedonia     19518
## 99                            Belize     13942
## 100                          Hungary     11740
## 101                           Angola     10000
## 103                       Azerbaijan      9397
## 104                             Fiji      8208
## 105         Central African Republic      6095
## 106                     South Africa      3076
## 107                  Solomon Islands      2745
## 108                           Brunei      2511
## 109                            Gabon      1700
## 110                         Zimbabwe      1336
## 111            Republic of the Congo      1211
## 112                          Somalia      1183
## 113                         Eswatini      1000
## 115                 Papua New Guinea       874
## 116              Trinidad and Tobago       734
## 117                          Algeria       300
## 118   Federated States of Micronesia       172

Chuẩn bị dataset world

Để vẽ đồ thị world map, chúng ta sẽ đưa dataset master này vào data frame world vốn là dữ liệu chứa thông tin địa lý các quốc gia trên thế giới.

library(ggplot2)
library(sf)
library(tidyverse)
library(ggrepel)
library(rnaturalearth)
library(rnaturalearthhires)
library(viridis)
library(ggspatial)
library(maps)
library(ggsn)

### Load dataset `world`
world <- ne_countries(scale = "medium", returnclass = "sf")

### Trong dataset `world` có một số quốc gia nhỏ chưa được update thông tin, vì vậy ta tách ra trong data_5
abcd <- c("East Timor", "Eswatini", "Ivory Coast", "Saint Kitts and Nevis", "Saint Vincent and the Grenadines", "Tokelau", "Tuvalu")
sep <- match(abcd, data_4$country)
data_5 <- data_4[-c(109, 31), ]

### Khớp lại tên các quốc gia trong `data_5` và `world`

# grep(pattern = "Antigua", world$name)
world$name[15] <- "Antigua and Barbuda"

# grep(pattern = "Bosnia", world$name)
world$name[27] <- "Bosnia and Herzegovina"

# grep(pattern = "Cook", world$name)
world$name[38] <- "Central African Republic"

# grep(pattern = "Central African", world$name)
world$name[47] <- "Cook Islands"

# grep(pattern = "Czech", world$name)
world$name[57] <- "Czech Republic" 

# grep(pattern = "Congo", world$name)
world$name[45] <- "Democratic Republic of the Congo"
world$name[46] <- "Republic of the Congo"

# grep(pattern = "Dominican", world$name)
world$name[62] <- "Dominican Republic" 

# grep(pattern = "Guinea", world$name)
world$name[84] <- "Equatorial Guinea"  

# grep(pattern = "Micronesia", world$name)
world$name[75] <- "Federated States of Micronesia"

# grep(pattern = "Polynesia", world$name)
world$name[181] <- "French Polynesia" 

# grep(pattern = "Lao", world$name)
world$name[122] <- "Laos"

# grep(pattern = "Korea", world$name)
world$name[177] <- "North Korea"
world$name[119] <- "South Korea"

# grep(pattern = "Macedonia", world$name) 
world$name[142] <- "North Macedonia"

# grep(pattern = "São", world$name) 
world$name[202] <- "São Tomé and Príncipe"

# grep(pattern = "Solomon", world$name) 
world$name[194] <- "Solomon Islands" 

# grep(pattern = "Sudan", world$name) 
world$name[189] <- "South Sudan"

### Kiểm tra toàn bộ tên các quốc gia trong `data_5` đã nằm trong `world` chưa? Kết quả là character(0) là OK.
setdiff(data_5$country, world$name)
## character(0)

Merge toàn bộ data_5 vào trong world để có thêm các cột liên quan đến sản lượng thực phẩm.

names(data_5)[1] <- "name"
merge(world, data_5, by = "name", all = TRUE) -> world_production

Vẽ đồ thị world map

Để xem hình độ phân giải cao, các bạn right-click vào hình, chọn Open image in new tab.

Sản lượng lúa gạo trên thế giới năm 2020

ggplot(data = world_production) +
    geom_sf(aes(fill = rice_2020/1000)) +
    xlab("Longitude") + ylab("Latitude") +
    scale_fill_viridis_c(name = "Đơn vị (nghìn tấn)",
                         option = "D", 
                         trans = "sqrt",
                         direction = 1,
                         na.value = "grey90",
                         guide = "colourbar") +
    ggtitle("Sản lượng lúa gạo trên thế giới năm 2020", subtitle = paste0("Nguồn: Wikipedia | Thực hiện: www.tuhocr.com")) +
    theme(legend.position = "right") +
    guides(fill = guide_colourbar(barwidth = 1, barheight = 20))

Vẽ đồ thị South-East Asia map

Danh sách region

t(t(table(world_production$subregion)))
##                            
##                             [,1]
##   Antarctica                   1
##   Australia and New Zealand    4
##   Caribbean                   25
##   Central America              8
##   Central Asia                 5
##   Eastern Africa              19
##   Eastern Asia                 8
##   Eastern Europe              10
##   Melanesia                    5
##   Micronesia                   7
##   Middle Africa                9
##   Northern Africa              7
##   Northern America             5
##   Northern Europe             15
##   Polynesia                    8
##   Seven seas (open ocean)      5
##   South-Eastern Asia          11
##   South America               13
##   Southern Africa              5
##   Southern Asia               10
##   Southern Europe             16
##   Western Africa              17
##   Western Asia                19
##   Western Europe               9

Tách dữ liệu theo region

south_asia <- subset(world_production, subregion == "South-Eastern Asia")
ggplot(data = south_asia) +
    geom_sf(aes(fill = rice_2020/1000)) +
    xlab("Longitude") + ylab("Latitude") +
    scale_fill_viridis_c(name = "Đơn vị (nghìn tấn)",
                         option = "D", 
                         trans = "sqrt",
                         direction = 1,
                         na.value = "grey90",
                         guide = "colourbar") +
    ggtitle("Sản lượng lúa gạo trên thế giới năm 2020", subtitle = paste0("Nguồn: Wikipedia | Thực hiện: www.tuhocr.com")) +
    theme(legend.position = "right") +
    guides(fill = guide_colourbar(barwidth = 1, barheight = 20))

# ggsave("img.png", height = 18, width = 30, units = 'in', dpi= 200, type = "cairo")

Làm đồ thị SEA map đẹp hơn

Chuẩn bị dữ liệu

# library(dplyr)
south_asia <- world_production %>% filter(subregion == "South-Eastern Asia") %>%
    select(name, subunit, geometry, rice_2020)

### tên quốc gia

library("sf")
world_points <- st_point_on_surface(world)

world_points <- cbind(world, st_coordinates(st_point_on_surface(world$geometry)))

world_points <- world_points %>% filter(subregion == "South-Eastern Asia")

## chỉnh vị trí tên quốc gia
world_points[9, "Y"] <- 16

world_points[11, "X"] <- 110

world_points[5, "Y"] <- 21

world_points[8, "Y"] <- 2
world_points[8, "X"] <- 107.5

world_points[10, "X"] <- 129
world_points[10, "Y"] <- -9.5

world_points[1, "Y"] <- 6

world_points[2, "X"] <- 109
world_points[2, "Y"] <- -4.5

world_points[7, "Y"] <- 12

# thailand <- subset(world_points, name == "Thailand")
# thailand$Y <- 15 

Lấy dữ liệu lúa gạo của các quốc gia Đông Nam Á

print(south_asia[, c(1, 3)], n = 30)
## Simple feature collection with 11 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 92.17958984375 ymin: -10.90966796875 xmax: 140.976171875 ymax: 28.517041015625
## Geodetic CRS:  +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
##           name rice_2020                       geometry
## 1       Brunei      2511 MULTIPOLYGON (((115.0267578...
## 2     Cambodia  10960000 MULTIPOLYGON (((103.3177734...
## 3    Indonesia  54649202 MULTIPOLYGON (((122.9489257...
## 4         Laos   3687336 MULTIPOLYGON (((102.1274414...
## 5     Malaysia   2321636 MULTIPOLYGON (((111.3892578...
## 6      Myanmar  25100000 MULTIPOLYGON (((98.18261718...
## 7  Philippines  19294856 MULTIPOLYGON (((120.2503906...
## 8    Singapore        NA MULTIPOLYGON (((103.9697265...
## 9     Thailand  30231025 MULTIPOLYGON (((99.66308593...
## 10 Timor-Leste     69000 MULTIPOLYGON (((124.3193359...
## 11     Vietnam  42758897 MULTIPOLYGON (((106.6174804...
rice_full_1 <- readRDS(file = "rice_full_1.rds")

unique(rice_full_1$area)
##   [1] "Afghanistan"                           "Albania"                               "Algeria"                               "Angola"                               
##   [5] "Argentina"                             "Australia"                             "Azerbaijan"                            "Bangladesh"                           
##   [9] "Belize"                                "Benin"                                 "Bhutan"                                "Bolivia (Plurinational State of)"     
##  [13] "Brazil"                                "Brunei Darussalam"                     "Bulgaria"                              "Burkina Faso"                         
##  [17] "Burundi"                               "Cambodia"                              "Cameroon"                              "Central African Republic"             
##  [21] "Chad"                                  "Chile"                                 "China, Hong Kong SAR"                  "China, Taiwan Province of"            
##  [25] "China, mainland"                       "Colombia"                              "Comoros"                               "Congo"                                
##  [29] "Costa Rica"                            "Cuba"                                  "Côte d'Ivoire"                         "Democratic People's Republic of Korea"
##  [33] "Democratic Republic of the Congo"      "Dominican Republic"                    "Ecuador"                               "Egypt"                                
##  [37] "El Salvador"                           "Eswatini"                              "Ethiopia"                              "Fiji"                                 
##  [41] "France"                                "French Guyana"                         "Gabon"                                 "Gambia"                               
##  [45] "Ghana"                                 "Greece"                                "Guatemala"                             "Guinea"                               
##  [49] "Guinea-Bissau"                         "Guyana"                                "Haiti"                                 "Honduras"                             
##  [53] "Hungary"                               "India"                                 "Indonesia"                             "Iran (Islamic Republic of)"           
##  [57] "Iraq"                                  "Italy"                                 "Jamaica"                               "Japan"                                
##  [61] "Kazakhstan"                            "Kenya"                                 "Kyrgyzstan"                            "Lao People's Democratic Republic"     
##  [65] "Liberia"                               "Madagascar"                            "Malawi"                                "Malaysia"                             
##  [69] "Mali"                                  "Mauritania"                            "Mauritius"                             "Mexico"                               
##  [73] "Micronesia (Federated States of)"      "Morocco"                               "Mozambique"                            "Myanmar"                              
##  [77] "Nepal"                                 "Nicaragua"                             "Niger"                                 "Nigeria"                              
##  [81] "North Macedonia"                       "Pakistan"                              "Panama"                                "Papua New Guinea"                     
##  [85] "Paraguay"                              "Peru"                                  "Philippines"                           "Portugal"                             
##  [89] "Puerto Rico"                           "Republic of Korea"                     "Romania"                               "Russian Federation"                   
##  [93] "Rwanda"                                "Réunion"                               "Saint Vincent and the Grenadines"      "Saudi Arabia"                         
##  [97] "Senegal"                               "Sierra Leone"                          "Solomon Islands"                       "Somalia"                              
## [101] "South Africa"                          "South Sudan"                           "Spain"                                 "Sri Lanka"                            
## [105] "Sudan"                                 "Sudan (former)"                        "Suriname"                              "Syrian Arab Republic"                 
## [109] "Tajikistan"                            "Thailand"                              "Timor-Leste"                           "Togo"                                 
## [113] "Trinidad and Tobago"                   "Turkmenistan"                          "Türkiye"                               "USSR"                                 
## [117] "Uganda"                                "Ukraine"                               "United Republic of Tanzania"           "United States of America"             
## [121] "Uruguay"                               "Uzbekistan"                            "Venezuela (Bolivarian Republic of)"    "Viet Nam"                             
## [125] "Yugoslav SFR"                          "Zambia"                                "Zimbabwe"
gsub(pattern = "Brunei Darussalam", replacement = "Brunei", rice_full_1$area, fixed = TRUE) -> rice_full_1$area

gsub(pattern = "Lao People's Democratic Republic", replacement = "Laos", rice_full_1$area, fixed = TRUE) -> rice_full_1$area

gsub(pattern = "Viet Nam", replacement = "Vietnam", rice_full_1$area, fixed = TRUE) -> rice_full_1$area

unique(rice_full_1$area)
##   [1] "Afghanistan"                           "Albania"                               "Algeria"                               "Angola"                               
##   [5] "Argentina"                             "Australia"                             "Azerbaijan"                            "Bangladesh"                           
##   [9] "Belize"                                "Benin"                                 "Bhutan"                                "Bolivia (Plurinational State of)"     
##  [13] "Brazil"                                "Brunei"                                "Bulgaria"                              "Burkina Faso"                         
##  [17] "Burundi"                               "Cambodia"                              "Cameroon"                              "Central African Republic"             
##  [21] "Chad"                                  "Chile"                                 "China, Hong Kong SAR"                  "China, Taiwan Province of"            
##  [25] "China, mainland"                       "Colombia"                              "Comoros"                               "Congo"                                
##  [29] "Costa Rica"                            "Cuba"                                  "Côte d'Ivoire"                         "Democratic People's Republic of Korea"
##  [33] "Democratic Republic of the Congo"      "Dominican Republic"                    "Ecuador"                               "Egypt"                                
##  [37] "El Salvador"                           "Eswatini"                              "Ethiopia"                              "Fiji"                                 
##  [41] "France"                                "French Guyana"                         "Gabon"                                 "Gambia"                               
##  [45] "Ghana"                                 "Greece"                                "Guatemala"                             "Guinea"                               
##  [49] "Guinea-Bissau"                         "Guyana"                                "Haiti"                                 "Honduras"                             
##  [53] "Hungary"                               "India"                                 "Indonesia"                             "Iran (Islamic Republic of)"           
##  [57] "Iraq"                                  "Italy"                                 "Jamaica"                               "Japan"                                
##  [61] "Kazakhstan"                            "Kenya"                                 "Kyrgyzstan"                            "Laos"                                 
##  [65] "Liberia"                               "Madagascar"                            "Malawi"                                "Malaysia"                             
##  [69] "Mali"                                  "Mauritania"                            "Mauritius"                             "Mexico"                               
##  [73] "Micronesia (Federated States of)"      "Morocco"                               "Mozambique"                            "Myanmar"                              
##  [77] "Nepal"                                 "Nicaragua"                             "Niger"                                 "Nigeria"                              
##  [81] "North Macedonia"                       "Pakistan"                              "Panama"                                "Papua New Guinea"                     
##  [85] "Paraguay"                              "Peru"                                  "Philippines"                           "Portugal"                             
##  [89] "Puerto Rico"                           "Republic of Korea"                     "Romania"                               "Russian Federation"                   
##  [93] "Rwanda"                                "Réunion"                               "Saint Vincent and the Grenadines"      "Saudi Arabia"                         
##  [97] "Senegal"                               "Sierra Leone"                          "Solomon Islands"                       "Somalia"                              
## [101] "South Africa"                          "South Sudan"                           "Spain"                                 "Sri Lanka"                            
## [105] "Sudan"                                 "Sudan (former)"                        "Suriname"                              "Syrian Arab Republic"                 
## [109] "Tajikistan"                            "Thailand"                              "Timor-Leste"                           "Togo"                                 
## [113] "Trinidad and Tobago"                   "Turkmenistan"                          "Türkiye"                               "USSR"                                 
## [117] "Uganda"                                "Ukraine"                               "United Republic of Tanzania"           "United States of America"             
## [121] "Uruguay"                               "Uzbekistan"                            "Venezuela (Bolivarian Republic of)"    "Vietnam"                              
## [125] "Yugoslav SFR"                          "Zambia"                                "Zimbabwe"
######### rice in SEA

sea_rice_data <- rice_full_1 |> subset(area == "Brunei" |
                                       area == "Cambodia" |
                                       area == "Indonesia" |
                                       area == "Laos" |
                                       area == "Malaysia" |
                                       area == "Myanmar" |       
                                       area == "Philippines" |
                                       area == "Thailand" |
                                       area == "Timor-Leste" |
                                       area == "Vietnam" 
                                       )

#####

sea_rice_data_2000 <- sea_rice_data |> subset(year == 2000) 

singapore_2000 <- data.frame(area = "Singapore", item = "Rice", year = 2000, 
                        value.production = NA,
                        value.area_harvested = NA)

sea_rice_data_2000 <- rbind(sea_rice_data_2000, singapore_2000)

sea_rice_data_2000 <- sea_rice_data_2000 |> dplyr::arrange(area)

row.names(sea_rice_data_2000) <- NULL

south_asia$rice_2000 <- sea_rice_data_2000$value.production

###############

sea_rice_data_2021 <- sea_rice_data |> subset(year == 2021) 

singapore_2021 <- data.frame(area = "Singapore", item = "Rice", year = 2021, 
                        value.production = NA,
                        value.area_harvested = NA)

sea_rice_data_2021 <- rbind(sea_rice_data_2021, singapore_2021)

sea_rice_data_2021 <- sea_rice_data_2021 |> dplyr::arrange(area)

row.names(sea_rice_data_2021) <- NULL

south_asia$rice_2021 <- sea_rice_data_2021$value.production
# extracting the capitals of SE Asia
capital <- maps::world.cities %>% 
    filter(country.etc %in% south_asia$subunit, capital == 1)


# convert capital to sf object
capital <- st_as_sf(capital, coords = c("long", "lat"), crs = 4326)
ggplot(data = world_production) +
    geom_sf() +
    geom_sf(data = south_asia, aes(fill = rice_2000/1000)) +
    xlab("Longitude") + ylab("Latitude") +
    scale_fill_distiller(palette = "YlGn", direction = 1,
                         name = "Đơn vị (nghìn tấn)",
                         na.value = "grey90",
                         guide = "colourbar") +
    # scale_fill_viridis_c(name = "Đơn vị (nghìn tấn)",
    #                      option = "plasma", 
    #                      trans = "sqrt",
    #                      direction = 1,
    #                      na.value = "grey90",
    #                      guide = "colourbar") +
    ggtitle("Sản lượng lúa gạo khu vực Đông Nam Á vào năm 2000", subtitle = paste0("Nguồn: FAOSTAT | Thực hiện: Duc Nguyen | tuhocr.com")) +
    theme(legend.position = "right") +
    theme(plot.title = element_text(face = "bold", colour = "darkgreen"),
          plot.subtitle = element_text(face = "bold", colour = "blue")) +
    guides(fill = guide_colourbar(barwidth = 1, barheight = 20)) +
    
    # adding capital cities as points
    geom_sf(data = capital, size = 1, color = "red") +
    
    geom_text(data = world_points, aes(x = X, y = Y, label = name),
              color = "darkblue", fontface = "bold", check_overlap = FALSE) +
    
    # setting the limits for South East Asia region
    coord_sf(xlim = c(88.594382881345, 152.93774260658),
             ylim = c(31.29186472309,-12.375471986864), expand = FALSE) +
    
    # adding scale and compass
    annotation_scale(location = "tr", pad_x = unit(0.5, "cm"), pad_y = unit(0.5, "cm")) +
  #   scalebar(data = south_asia, location = "topleft", dist = 500,
  # dist_unit = "km", transform = TRUE,  model = "International") +
    # annotation_north_arrow(location = "tr", which_north = "true", 
    #                        pad_x = unit(0.3, "in"), pad_y = unit(0.5, "in"),
    #                        style = north_arrow_fancy_orienteering) +
    ggspatial::annotation_north_arrow(
    location = "tr", which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20",
      text_family = "ArcherPro Book"
    )) +
    annotate(geom = "text", x = 113.5, y = 12, label = "South East \nAsian Sea", 
        fontface = "italic", color = "grey22", size = 4) +
    
    # adding color the waters and other aesthetics
    theme(panel.background = element_rect(fill = "aliceblue"))

library(png) ## dùng đề chèn file ảnh
library(grid) ## canh chỉnh vị trí ảnh
logor <- readPNG("logo-blue.png")
grid.raster(logor, x = 0.91, y = 0.89, width = 0.1)

ggplot(data = world_production) +
    geom_sf() +
    geom_sf(data = south_asia, aes(fill = rice_2021/1000)) +
    xlab("Longitude") + ylab("Latitude") +
    scale_fill_distiller(palette = "YlGn", direction = 1,
                         name = "Đơn vị (nghìn tấn)",
                         na.value = "grey90",
                         guide = "colourbar") +
    # scale_fill_viridis_c(name = "Đơn vị (nghìn tấn)",
    #                      option = "plasma", 
    #                      trans = "sqrt",
    #                      direction = 1,
    #                      na.value = "grey90",
    #                      guide = "colourbar") +
    ggtitle("Sản lượng lúa gạo khu vực Đông Nam Á vào năm 2021", subtitle = paste0("Nguồn: FAOSTAT | Thực hiện: Duc Nguyen | tuhocr.com")) +
    theme(legend.position = "right") +
    theme(plot.title = element_text(face = "bold", colour = "red"),
          plot.subtitle = element_text(face = "bold", colour = "blue")) +
    guides(fill = guide_colourbar(barwidth = 1, barheight = 20)) +
    
    # adding capital cities as points
    geom_sf(data = capital, size = 1, color = "red") +
    
    geom_text(data = world_points, aes(x = X, y = Y, label = name),
              color = "darkblue", fontface = "bold", check_overlap = FALSE) +
    
    # setting the limits for South East Asia region
    coord_sf(xlim = c(88.594382881345, 152.93774260658),
             ylim = c(31.29186472309,-12.375471986864), expand = FALSE) +
    
    # adding scale and compass
    annotation_scale(location = "tr", pad_x = unit(0.5, "cm"), pad_y = unit(0.5, "cm")) +
  #   scalebar(data = south_asia, location = "topleft", dist = 500,
  # dist_unit = "km", transform = TRUE,  model = "International") +
    # annotation_north_arrow(location = "tr", which_north = "true", 
    #                        pad_x = unit(0.3, "in"), pad_y = unit(0.5, "in"),
    #                        style = north_arrow_fancy_orienteering) +
    ggspatial::annotation_north_arrow(
    location = "tr", which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20",
      text_family = "ArcherPro Book"
    )) +
    annotate(geom = "text", x = 113.5, y = 12, label = "South East \nAsian Sea", 
        fontface = "italic", color = "grey22", size = 4) +
    
    # adding color the waters and other aesthetics
    theme(panel.background = element_rect(fill = "aliceblue"))

library(png) ## dùng đề chèn file ảnh
library(grid) ## canh chỉnh vị trí ảnh
logor <- readPNG("logo-blue.png")
grid.raster(logor, x = 0.91, y = 0.89, width = 0.1)

Các bạn cũng có thể đối chiếu kết quả này với các đồ thị world map từ

Tài liệu tham khảo

  1. http://aejaffe.com/summerR_2015/modules/mapping_module.html

  2. https://blog.rwhitedwarf.com/post/map_any_region_with_ggplot2_part_ii/

  3. https://medium.com/geekculture/how-to-map-data-with-r-8333110dff5b

  4. https://r-spatial.org/r/2018/10/25/ggplot2-sf.html

Sơ kết

Trên đây là hướng dẫn vẽ heatmap theo khu vực địa lý. Để học R bài bản từ A đến Z, thân mời Bạn tham gia khóa học “HDSD R để xử lý dữ liệu” để có nền tảng vững chắc về R nhằm tự tay làm các câu chuyện dữ liệu của riêng mình!

ĐĂNG KÝ NGAY: https://www.tuhocr.com/register

Hướng dẫn cài đặt package tuhocr https://tuhocr.github.io/