Import dataset

Sử dụng lệnh IMPORTHTML() trong googlesheet để import bảng dữ liệu từ wikipedia vào file googlesheet.

Tham khảo Scrape data from wikipedia and put into Google Sheets by Chris Menard.

Các file googlesheet tương ứng:

Load dataset vào R

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

coffee_production <- read_sheet('1gKRpLrMr1Tg7PbFCBn5mFQXPSdZf5oPPcch0F8B6E6A')

rice_production <- read_sheet('11EU8qJt14EflHgU2cCENQnIJUZKIClAMWHaFa_CEchk')

fruit_production <- read_sheet('18axSqCjxQq6o7-cfKeAQN8WmROBX9QiInApBdIApOkg')

vegetable_production <- read_sheet('1NHsdl7_bWhRjsFIwVvQX5WpGHtLEMeX6D7qFdCZReMk')

soybean_production <- read_sheet('19m2pofqWczmQlRSkZ582j-ljb-dYFpa1_KxxJIcuags')

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 coffee_production
coffee_production_df <- as.data.frame(coffee_production)
coffee_production_2019 <- coffee_production_df[, c(2, 4)] # chọn những cột quan tâm

### Làm sạch data
gsub(pattern = "China(2013–14 est.)[7]", replacement = "China", x = coffee_production_2019[, 1], fixed = TRUE) -> coffee_production_2019[, 1]
gsub(pattern = "Timor Leste", replacement = "Timor-Leste", x = coffee_production_2019[, 1], fixed = TRUE) -> coffee_production_2019[, 1]

names(coffee_production_2019) <- c("country", "coffee_2019")

### In dataset
head(coffee_production_2019, n = 10)
##      country coffee_2019
## 1     Brazil     2652000
## 2    Vietnam     1650000
## 3   Colombia      810000
## 4  Indonesia      660000
## 5   Ethiopia      384000
## 6   Honduras      348000
## 7      India      348000
## 8     Uganda      288000
## 9     Mexico      234000
## 10 Guatemala      204000
### 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
## 1        China 211860000
## 2        India 178305000
## 3   Bangladesh  54905891
## 4    Indonesia  54649202
## 5      Vietnam  42758897
## 6     Thailand  30231025
## 7      Myanmar  25100000
## 8  Philippines  19294856
## 9       Brazil  11091011
## 10    Cambodia  10960000
### Cleaning dataset fruit_production
fruit_production_df <- as.data.frame(fruit_production)
fruit_production_2020 <- fruit_production_df[, c(2, 3)] # chọn những cột quan tâm

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

### Làm sạch data
gsub(pattern = "People's Republic of China", replacement = "China", x = fruit_production_2020[, 1], fixed = TRUE) -> fruit_production_2020[, 1]

which(fruit_production_2020[, 1] == "Saint Kitts and Nevis") -> remove_1
fruit_production_2020[-remove_1, ] -> fruit_production_2020

names(fruit_production_2020) <- c("country", "fruit_2020")

### In dataset
head(fruit_production_2020, n = 10)
##          country fruit_2020
## 1          China  242793824
## 2          India  105971127
## 3         Brazil   39758842
## 4         Turkey   24153128
## 5         Mexico   23837562
## 6  United States   23747765
## 7      Indonesia   22743965
## 8          Spain   19471070
## 9           Iran   18963596
## 10         Italy   17827510
### Cleaning dataset vegetable_production
vegetable_production_df <- as.data.frame(vegetable_production)
vegetable_production_2020 <- vegetable_production_df[, c(2, 3)] # chọn những cột quan tâm

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

### Làm sạch data
gsub(pattern = "People's Republic of China", replacement = "China", x = vegetable_production_2020[, 1], fixed = TRUE) -> vegetable_production_2020[, 1]

names(vegetable_production_2020) <- c("country", "vegetable_2020")

### In dataset
head(vegetable_production_2020, n = 10)
##          country vegetable_2020
## 1          China      594049398
## 2          India      141195036
## 3  United States       33124467
## 4         Turkey       25960714
## 5        Vietnam       17002195
## 6          Egypt       16135024
## 7        Nigeria       15706483
## 8         Mexico       15098212
## 9         Russia       13950679
## 10         Spain       12668790
### Cleaning dataset soybean_production
soybean_production_df <- as.data.frame(soybean_production)
soybean_production_2020 <- soybean_production_df[, c(2, 3)] # chọn những cột quan tâm

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

### Làm sạch data
which(soybean_production_2020[, 1] == "Country") -> remove_2
soybean_production_2020[-remove_2, ] -> soybean_production_2020

names(soybean_production_2020) <- c("country", "soybean_2020")

### In dataset
head(soybean_production_2020, n = 10)
##          country soybean_2020
## 1         Brazil    121797712
## 2  United States    112549240
## 3      Argentina     48796661
## 4          China     19600000
## 5          India     11226000
## 6       Paraguay     11024460
## 7         Canada      6358500
## 8         Russia      4307593
## 9        Bolivia      2829356
## 10       Ukraine      2797670

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)

merge(coffee_production_2019, rice_production_2020, by = "country", all = TRUE) -> data_1

merge(data_1, fruit_production_2020, by = "country", all = TRUE) -> data_2

merge(data_2, vegetable_production_2020, by = "country", all = TRUE) -> data_3

merge(data_3, soybean_production_2020, by = "country", all = TRUE) -> data_4

### In dataset master
dim(data_4) # có 197 quốc gia và vùng lãnh thổ trên thế giới
## [1] 197   6
data_4
##                              country coffee_2019 rice_2020 fruit_2020 vegetable_2020 soybean_2020
## 1                        Afghanistan          NA    439549    3459051        1822632           NA
## 2                            Albania          NA        NA     818946         988507          441
## 3                            Algeria          NA       300    7055092        7986465           NA
## 4                             Angola        2100     10000    5174650         743741        40797
## 5                Antigua and Barbuda          NA        NA       8726            847           NA
## 6                          Argentina          NA   1222910    7441342        3209268     48796661
## 7                            Armenia          NA        NA     680216         599918           NA
## 8                          Australia          NA     50226    3539850        1588764        17323
## 9                            Austria          NA        NA     699860         588640       204860
## 10                        Azerbaijan          NA      9397    1685881        1740427           29
## 11                           Bahamas          NA        NA      48413          33388           NA
## 12                           Bahrain          NA        NA      21284          18179           NA
## 13                        Bangladesh          NA  54905891    5027255        7138722       104761
## 14                          Barbados          NA        NA       5146          10563           NA
## 15                           Belarus          NA        NA     776183        1782395           NA
## 16                           Belgium          NA        NA     615930        2095600           NA
## 17                            Belize          NA     13942     211576          11854        13670
## 18                             Benin          NA    411578     627319         702795       253954
## 19                            Bhutan          NA     54088      56475          48273          234
## 20                           Bolivia        5400    487427    1540081         392752      2829356
## 21            Bosnia and Herzegovina          NA        NA     432324         799683        37202
## 22                          Botswana          NA        NA      32152          81439           NA
## 23                            Brazil     2652000  11091011   39758842        8373666    121797712
## 24                            Brunei          NA      2511       8133           9858           NA
## 25                          Bulgaria          NA     65810     464940         340500         6200
## 26                      Burkina Faso          NA    395443     104416         302869        48000
## 27                           Burundi       12000    150000    1723465         482675         2571
## 28                          Cambodia          NA  10960000     372535         624613       180000
## 29                          Cameroon       34200    328503    6410141        3051807        24195
## 30                            Canada          NA        NA     925776        2241369      6358500
## 31                        Cape Verde          NA        NA       8366          30500           NA
## 32          Central African Republic        3900      6095     310775          95169           NA
## 33                              Chad          NA    278053     125896         110564           NA
## 34                             Chile          NA    169697    6779886        2134555           NA
## 35                             China      116820 211860000  242793824      594049398     19600000
## 36                          Colombia      810000   3424119   10521546        2450822       119412
## 37                           Comoros          NA     30543      50526           5295           NA
## 38                      Cook Islands          NA        NA       1269           1880           NA
## 39                        Costa Rica       89520    137504    6190882         177115           NA
## 40                           Croatia          NA        NA     281670         166310       266010
## 41                              Cuba        6000    266595    1697454        1628116           NA
## 42                            Cyprus          NA        NA     126480          49940           NA
## 43                    Czech Republic          NA        NA     233610         203050        33020
## 44  Democratic Republic of the Congo       20100   1379000    6747921         591691        26000
## 45                           Denmark          NA        NA      58060         253830           NA
## 46                          Djibouti          NA        NA       4873          35486           NA
## 47                          Dominica          NA        NA      51623           7145           NA
## 48                Dominican Republic       24000    942765    5670664         873844           NA
## 49                        East Timor          NA        NA         NA             NA          978
## 50                           Ecuador       42000   1336502    7630370         421052        27238
## 51                             Egypt          NA   4893507   14733617       16135024        50000
## 52                       El Salvador       45720     28000     316695         121223         5084
## 53                 Equatorial Guinea          NA        NA      70299             NA           NA
## 54                           Eritrea          NA        NA       4853          54883           NA
## 55                           Estonia          NA        NA       4190          26810           NA
## 56                          Eswatini          NA      1000     146442          13154           NA
## 57                          Ethiopia      384000    189649    1558021        1622146       208676
## 58    Federated States of Micronesia          NA       172       2628           3543           NA
## 59                              Fiji          NA      8208      40895          55316           NA
## 60                           Finland          NA        NA      25550         270240           NA
## 61                            France          NA     76320    8887220        4422070       406670
## 62                  French Polynesia          NA        NA      17218           5011           NA
## 63                             Gabon       30000      1700     387085          50018         4203
## 64                            Gambia          NA     28000       9503          12828           NA
## 65                           Georgia          NA        NA     633400         176100         2000
## 66                           Germany          NA        NA    2501450        3437190        90500
## 67                             Ghana        2220    973000    6350716         788396       177007
## 68                            Greece          NA    287410    4436280        1918930         1840
## 69                           Grenada          NA        NA      19111           6438           NA
## 70                         Guatemala      204000     32000    6898708        1238156        41000
## 71                            Guinea        9600   2916395    1317847         561210           NA
## 72                     Guinea-Bissau          NA    198000     109077          39426           NA
## 73                            Guyana          NA    687539     327946         194361           NA
## 74                             Haiti       21000    172000    1169114         152995           NA
## 75                          Honduras      348000     65634    1548940         363892         1444
## 76                         Hong Kong          NA        NA       5088          36371           NA
## 77                           Hungary          NA     11740    1164360         666410       165760
## 78                           Iceland          NA        NA         NA           4650           NA
## 79                             India      348000 178305000  105971127      141195036     11226000
## 80                         Indonesia      660000  54649202   22743965       12581898      1040000
## 81                              Iran          NA   2000000   18963596       12623192       140000
## 82                              Iraq          NA    464159    2160646        1733642           33
## 83                           Ireland          NA        NA      26150         187590           NA
## 84                            Israel          NA        NA    1419730        1247103           NA
## 85                             Italy          NA   1507490   17827510       10849360      1005630
## 86                       Ivory Coast      108000   1481182    2891070         770581          401
## 87                           Jamaica        1260        NA     373962         266000           NA
## 88                             Japan          NA   9706250    2929692       10221895       218900
## 89                            Jordan          NA        NA     558727        1401350           NA
## 90                        Kazakhstan          NA    556775    2863721        4450783       260639
## 91                             Kenya       49980    180890    4373793        3602389         2396
## 92                          Kiribati          NA        NA       9024           5993           NA
## 93                            Kuwait          NA        NA     124522         376740           NA
## 94                        Kyrgyzstan          NA     44474     457024        1101996         2371
## 95                              Laos       31200   3687336    1146352        1533045        14550
## 96                            Latvia          NA        NA      19500          68500           NA
## 97                           Lebanon          NA        NA    1036334         680318           NA
## 98                           Lesotho          NA        NA      15350          31936           NA
## 99                           Liberia         360    270000     205631         120806         3417
## 100                            Libya          NA        NA     685416         688745           NA
## 101                        Lithuania          NA        NA      68580         164250         2560
## 102                       Luxembourg          NA        NA      16040           3460           30
## 103                       Madagascar       31200   4232000    1259165         455672           46
## 104                           Malawi         960    145446    3713852        1824553       180000
## 105                         Malaysia          NA   2321636    1113002        1171544           NA
## 106                         Maldives          NA        NA       6144           2611           NA
## 107                             Mali          NA   3010027    2350297        2535287        14685
## 108                            Malta          NA        NA      10900          40050           NA
## 109                       Mauritania          NA    365000      29055           4794           NA
## 110                        Mauritius          NA        NA      24434          56606           NA
## 111                           Mexico      234000    295338   23837562       15098212       246019
## 112                          Moldova          NA        NA    1141221         222946        33360
## 113                         Mongolia          NA        NA       1600         121569           NA
## 114                       Montenegro          NA        NA      73505          22564           NA
## 115                          Morocco          NA     46275    5586937        3983906          727
## 116                       Mozambique          NA    137243    1138328         981152        75000
## 117                          Myanmar          NA  25100000    2732463        4820496       145000
## 118                          Namibia          NA        NA      61891          65405           NA
## 119                            Nauru          NA        NA        474            465           NA
## 120                            Nepal          NA   5550878    1503925        4169485        37526
## 121                      Netherlands          NA        NA     738770        5293140           NA
## 122                    New Caledonia          NA        NA      12552           8749           NA
## 123                      New Zealand          NA        NA    1780310         886443           NA
## 124                        Nicaragua      132000    476992     581833         236147        10000
## 125                            Niger          NA    179382     636912        3333839           NA
## 126                          Nigeria        2400   8172000   11529922       15706483       600000
## 127                             Niue          NA        NA        906            143           NA
## 128                      North Korea          NA   2113019    1823821        3318019       229892
## 129                  North Macedonia          NA     19518     635961         695920           18
## 130                           Norway          NA        NA      23836         185195           NA
## 131                             Oman          NA        NA     505249         836190           NA
## 132                         Pakistan          NA   8419276    9825573        5572793          152
## 133                        Palestine          NA        NA     109350         465434           NA
## 134                           Panama        6000    391600     644813          51842          100
## 135                 Papua New Guinea       48000       874    2467868         560596           NA
## 136                         Paraguay        1200   1187768     776987         153840     11024460
## 137                             Peru      192000   3436637    7375819        2943057         1538
## 138                      Philippines       12000  19294856   16482063        6624100          554
## 139                           Poland          NA        NA    4499680        4320800        14940
## 140                         Portugal          NA    132790    2009626        2288910           NA
## 141                      Puerto Rico          NA       138     208542          39838           NA
## 142                            Qatar          NA        NA      29144          71035           NA
## 143            Republic of the Congo          NA      1211     269986         142528           NA
## 144                          Romania          NA     24670    2953850        1798720       353640
## 145                           Russia          NA   1141819    5906985       13950679      4307593
## 146                           Rwanda       15000    116504    2176865         661274        23755
## 147            Saint Kitts and Nevis          NA        NA         NA            687           NA
## 148                      Saint Lucia          NA        NA         NA           3179           NA
## 149 Saint Vincent and the Grenadines          NA        NA      77252           5056           NA
## 150                            Samoa          NA        NA      46538           1318           NA
## 151            São Tomé and Príncipe          NA        NA      50601           3691           NA
## 152                     Saudi Arabia          NA        NA    2913925        1052171           NA
## 153                          Senegal          NA   1349723    2008315        1000629           NA
## 154                           Serbia          NA        NA    1909656         669062       751578
## 155                       Seychelles          NA        NA       2964           3001           NA
## 156                     Sierra Leone        2160   1049795     269722         468497           NA
## 157                        Singapore          NA        NA          5          25569           NA
## 158                         Slovakia          NA        NA      85120         106930       132200
## 159                         Slovenia          NA        NA     186100         133108         5020
## 160                  Solomon Islands          NA      2745      30094           6042           NA
## 161                          Somalia          NA      1183     214348         104260           NA
## 162                     South Africa          NA      3076    7456699        2636219      1245500
## 163                      South Korea          NA   4713162    2765000        9476881        80926
## 164                      South Sudan          NA        NA     553035         496999           NA
## 165                            Spain          NA    739230   19471070       12668790         4620
## 166                        Sri Lanka          NA   5120924    1339584        1306938         7879
## 167                            Sudan          NA     34133    3263482        3868342           NA
## 168                         Suriname          NA    285712      97803          22204           12
## 169                           Sweden          NA        NA      49440         342880           NA
## 170                      Switzerland          NA        NA     373177         399411         5247
## 171                            Syria          NA        NA    2449998        2088946         6227
## 172                           Taiwan          NA   1750729    2653885        2080502         4447
## 173                       Tajikistan          NA     99000    1342138        2374585           21
## 174                         Tanzania       48000   4528000    5664181        2772533        23886
## 175                         Thailand       30000  30231025   10098175        2820271        29231
## 176                      Timor-Leste        4800     69000      17218          32945           NA
## 177                             Togo       12000    160000      66823         149504         1796
## 178                          Tokelau          NA        NA         76             NA           NA
## 179                            Tonga          NA        NA       8611          27320           NA
## 180              Trinidad and Tobago         720       734      74453          18420           NA
## 181                          Tunisia          NA        NA    2389063        3138362           NA
## 182                           Turkey          NA    980000   24153128       25960714       155225
## 183                     Turkmenistan          NA    150000     683563         678063           NA
## 184                           Tuvalu          NA        NA        869            596           NA
## 185                           Uganda      288000    200000    7457778        1380470        75077
## 186                          Ukraine          NA     60680    2688105        9675389      2797670
## 187             United Arab Emirates          NA        NA     361471         233009           NA
## 188                   United Kingdom          NA        NA     738454        2525470           NA
## 189                    United States          NA  10322990   23747765       33124467    112549240
## 190                          Uruguay          NA   1209000     395578         174117      1990000
## 191                       Uzbekistan          NA    293451    5824616        9903740         7460
## 192                          Vanuatu          NA        NA      22290          13273           NA
## 193                        Venezuela       30000    429179    3558935        1221274         9306
## 194                          Vietnam     1650000  42758897   10616559       17002195        65405
## 195                            Yemen        7200        NA    1173553         480563           NA
## 196                           Zambia         120     34630     116340         434774       296866
## 197                         Zimbabwe         600      1336     358622         228485        59656

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("rnaturalearth")
library("rnaturalearthdata")

### 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[-sep, ]

### 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 cà phê trên thế giới năm 2019

# nếu muốn in file PDF có tiếng việt hoàn chỉnh thì dùng lệnh 
# cairo_pdf("coffee_2019.pdf", width = 20, height = 10)

ggplot(data = world_production) +
    geom_sf(aes(fill = coffee_2019/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 cà phê trên thế giới năm 2019", subtitle = paste0("Nguồn: Wikipedia | Thực hiện: www.tuhocr.com")) +
    theme(legend.position = "right") +
    guides(fill = guide_colourbar(barwidth = 1, barheight = 20))

# dev.off()

Sản lượng 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 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))

Sản lượng trái cây trên thế giới năm 2020

ggplot(data = world_production) +
    geom_sf(aes(fill = fruit_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 trái cây 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))

Sản lượng rau củ quả trên thế giới năm 2020

ggplot(data = world_production) +
    geom_sf(aes(fill = vegetable_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 rau củ quả 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))

Sản lượng đậu nành trên thế giới năm 2020

ggplot(data = world_production) +
    geom_sf(aes(fill = soybean_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 đậu nành 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))

Các bạn cũng có thể đối chiếu kết quả này với các đồ thị world map từ https://www.visualcapitalist.com/cp/mapped-food-production-around-the-world/

Sơ kết

Trên đây là hướng dẫn cách lấy dữ liệu từ Wikipedia và vẽ world map trong R. Để học R bài bản từ A đến Z, kính 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!

Nội dung khóa học: www.tuhocr.com

Hành trình ngàn dặm bắt đầu từ bước chân đầu tiên.

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

Session info

sessionInfo()
## R version 4.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                           LC_TIME=English_United States.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] rnaturalearthdata_0.1.0 rnaturalearth_0.1.0     sf_1.0-8                ggplot2_3.4.0           googlesheets4_1.0.1    
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.1.2   xfun_0.33          bslib_0.4.0        purrr_0.3.4        lattice_0.20-45    gargle_1.2.1       colorspace_2.0-3   vctrs_0.5.1        generics_0.1.3     viridisLite_0.4.1 
## [11] htmltools_0.5.3    s2_1.1.0           yaml_2.3.5         utf8_1.2.2         rlang_1.0.6        e1071_1.7-11       jquerylib_0.1.4    pillar_1.8.1       glue_1.6.2         withr_2.5.0       
## [21] DBI_1.1.3          rappdirs_0.3.3     sp_1.5-1           wk_0.6.0           lifecycle_1.0.3    stringr_1.4.1      munsell_0.5.0      gtable_0.3.1       cellranger_1.1.0   evaluate_0.16     
## [31] labeling_0.4.2     knitr_1.40         fastmap_1.1.0      curl_4.3.2         class_7.3-20       fansi_1.0.3        highr_0.9          Rcpp_1.0.9         KernSmooth_2.23-20 openssl_2.0.3     
## [41] scales_1.2.1       classInt_0.4-7     cachem_1.0.6       jsonlite_1.8.0     farver_2.1.1       fs_1.5.2           askpass_1.1        digest_0.6.29      stringi_1.7.8      dplyr_1.0.10      
## [51] grid_4.2.1         cli_3.4.0          tools_4.2.1        magrittr_2.0.3     sass_0.4.2         proxy_0.4-27       tibble_3.1.8       pkgconfig_2.0.3    googledrive_2.0.0  assertthat_0.2.1  
## [61] rmarkdown_2.16     httr_1.4.4         rstudioapi_0.14    R6_2.5.1           units_0.8-0        compiler_4.2.1