Các bạn sử dụng package FAOSTAT để download dataset về và subset ra dữ liệu về lúa gạo. Ở đây mình đã làm công đoạn cleaning data nên chỉ cần import vào để đưa code xuất ra bảng xếp hạng.

library(kableExtra)
library(formattable)
load("top20_rice.Rdata")
# options(width = 200)

top20_rice -> rice_topten

rice_compare <- subset(rice_topten, year == 2020 | year == 2000)

rice_compare 
##                                        area item year production area_harvested
## 694                              Bangladesh Rice 2020   54905891       11417745
## 754                              Bangladesh Rice 2000   37627500       10801214
## 1382                                 Brazil Rice 2020   11091011        1677705
## 1442                                 Brazil Rice 2000   11134588        3664804
## 2062                               Cambodia Rice 2020   11248000        3322800
## 2110                               Cambodia Rice 2000    4026092        1903159
## 2995                        China, mainland Rice 2020  211860000       30080000
## 3055                        China, mainland Rice 2000  187908000       29961890
## 3817  Democratic People's Republic of Korea Rice 2020    2113019         470295
## 3873  Democratic People's Republic of Korea Rice 2000    1690000         535000
## 4346                                  Egypt Rice 2020    4804000         499361
## 4406                                  Egypt Rice 2000    6000490         659217
## 6417                                  India Rice 2020  186500000       45070000
## 6477                                  India Rice 2000  127464896       44712000
## 6570                              Indonesia Rice 2020   54649202       10657275
## 6599                              Indonesia Rice 2000   51898000       11793000
## 7246                                  Japan Rice 2020   10469000        1462000
## 7290                                  Japan Rice 2000   11863000        1770000
## 7964                             Madagascar Rice 2020    4227927        1674540
## 8010                             Madagascar Rice 2000    2480470        1209300
## 9111                                Myanmar Rice 2020   25982696        6829904
## 9171                                Myanmar Rice 2000   20986900        6302175
## 9264                                  Nepal Rice 2020    5550878        1458915
## 9324                                  Nepal Rice 2000    4216465        1560044
## 9715                                Nigeria Rice 2020    8171800        4195100
## 9775                                Nigeria Rice 2000    3298000        2199000
## 9953                               Pakistan Rice 2020   12629514        3335105
## 10013                              Pakistan Rice 2000    7203900        2376600
## 10583                           Philippines Rice 2020   19294856        4718896
## 10643                           Philippines Rice 2000   12389412        4038085
## 10924                     Republic of Korea Rice 2020    4713162         726432
## 10984                     Republic of Korea Rice 2000    7196582        1072363
## 12097                             Sri Lanka Rice 2020    5120924        1066403
## 12155                             Sri Lanka Rice 2000    2859900         832000
## 12655                              Thailand Rice 2020   30231025       10401653
## 12713                              Thailand Rice 2000   25843878        9891200
## 13759              United States of America Rice 2020   10319860        1208400
## 13819              United States of America Rice 2000    8657810        1229850
## 14305                              Viet Nam Rice 2020   42765000        7222403
## 14365                              Viet Nam Rice 2000   32529500        7666300
row.names(rice_compare) <- NULL

rice_compare <- rice_compare[- c(2)]

rice_compare$yield <- round(rice_compare$production / rice_compare$area_harvested,
                            digits = 2)

rice_compare$production <- round(rice_compare$production / 1000,
                            digits = 0)

rice_compare$area_harvested <- round(rice_compare$area_harvested / 1000,
                                 digits = 0)

rice_wide <- reshape(data = rice_compare,
                     idvar = c("area"),
                     # v.names = c("production"),
                     timevar = "year",
                     direction = "wide") 


rice_wide <- rice_wide[c(1, 5, 2, 6, 3, 7, 4)]

rice_wide$percent_1 <- round(100 * (rice_wide$production.2020 - rice_wide$production.2000) / rice_wide$production.2000, digits = 2)

rice_wide$percent_2 <- round(100 * (rice_wide$area_harvested.2020 - rice_wide$area_harvested.2000) / rice_wide$area_harvested.2000, digits = 2)

rice_wide$percent_3 <- round(100 * (rice_wide$yield.2020 - rice_wide$yield.2000) / rice_wide$yield.2000, digits = 2)

rice_wide <- rice_wide[c(1, 2, 3, 8, 4, 5, 9, 6, 7, 10)]

rank_1 <- order(rice_wide$production.2020, decreasing = TRUE)

rice_wide <- rice_wide[rank_1, ]

row.names(rice_wide) <- NULL

rice_wide -> rice_wide_ok

rice_wide_ok -> rice_wide

###

rice_wide$production.2020 <- color_bar("lightgreen")(rice_wide$production.2020)
rice_wide$production.2000 <- color_bar("lightgreen")(rice_wide$production.2000)

rice_wide$area_harvested.2020 <- color_bar("orange")(rice_wide$area_harvested.2020)
rice_wide$area_harvested.2000 <- color_bar("orange")(rice_wide$area_harvested.2000)

rice_wide$yield.2020 <- color_bar("pink")(rice_wide$yield.2020)
rice_wide$yield.2000 <- color_bar("pink")(rice_wide$yield.2000)

rice_wide$percent_1 <- color_tile("white", "lightgreen")(rice_wide$percent_1)

rice_wide$percent_2 <- color_tile("white", "orange")(rice_wide$percent_2)

rice_wide$percent_3 <- color_tile("white", "pink")(rice_wide$percent_3)

file_image <- list.files("png", full.names = TRUE)

gsub(".png", "", basename(file_image))[order(match(gsub(".png", "", basename(file_image)), 
                                                   rice_wide$area))] -> file_image_2

file_image_3 <- paste0("png/", file_image_2, ".png")

rice_wide$rank <- 1:20

rice_wide$flag <- ""

rice_wide[c(11, 1, 2:10)] -> rice_wide

# rice_wide[c(12, 1:11)] -> rice_wide

names(rice_wide) <- c("Xếp hạng", "Quốc gia", "Năm 2000", "Năm 2020", "Tỷ lệ \ntăng trưởng (%)",
                      "Năm 2000", "Năm 2020", "Tỷ lệ \ntăng trưởng  (%)",
                      "Năm 2000", "Năm 2020", "Tỷ lệ \ntăng trưởng  (%)")

colnames(rice_wide) <- stringr::str_replace_all(colnames(rice_wide), "\\n", "<br>")

kbl(rice_wide, escape = FALSE, caption = "<center><strong><span style='color: blue'> Top 20 quốc gia sản xuất lúa gạo trên thế giới | Dữ liệu FAOSTAT | Minh họa: tuhocr.com</span></strong></center>",
    format = "html") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive",
                                      position = "float_left")) %>%
  # kbl(caption = "Recreating booktabs style table") %>%
  kable_classic(full_width = TRUE, html_font = "arial") %>%
  # kable_paper("hover", full_width = TRUE) %>%
  column_spec(1, width = "2.5cm") %>%
  column_spec(2, width = "8cm", border_right = TRUE) %>%
  column_spec(5, border_right = TRUE) %>%
  column_spec(8, border_right = TRUE) %>%
  column_spec(1, image = spec_image(file_image_3 , 96, 72)) %>%
  row_spec(0:20, bold = TRUE) %>%
  row_spec(5, background = "yellow") %>% 
  add_header_above(c("", "", "Sản lượng thu hoạch (nghìn tấn)" = 3, "Diện tích canh tác (nghìn ha)" = 3, "Năng suất (tấn/ha)" = 3),
                   bold = TRUE)
Top 20 quốc gia sản xuất lúa gạo trên thế giới | Dữ liệu FAOSTAT | Minh họa: tuhocr.com
Sản lượng thu hoạch (nghìn tấn)
Diện tích canh tác (nghìn ha)
Năng suất (tấn/ha)
Xếp hạng Quốc gia Năm 2000 Năm 2020 Tỷ lệ
tăng trưởng (%)
Năm 2000 Năm 2020 Tỷ lệ
tăng trưởng (%)
Năm 2000 Năm 2020 Tỷ lệ
tăng trưởng (%)
1 China, mainland 187908 211860 12.75 29962 30080 0.39 6.27 7.04 12.28
2 India 127465 186500 46.31 44712 45070 0.80 2.85 4.14 45.26
3 Bangladesh 37628 54906 45.92 10801 11418 5.71 3.48 4.81 38.22
4 Indonesia 51898 54649 5.30 11793 10657 -9.63 4.40 5.13 16.59
5 Viet Nam 32530 42765 31.46 7666 7222 -5.79 4.24 5.92 39.62
6 Thailand 25844 30231 16.97 9891 10402 5.17 2.61 2.91 11.49
7 Myanmar 20987 25983 23.81 6302 6830 8.38 3.33 3.80 14.11
8 Philippines 12389 19295 55.74 4038 4719 16.86 3.07 4.09 33.22
9 Pakistan 7204 12630 75.32 2377 3335 40.30 3.03 3.79 25.08
10 Cambodia 4026 11248 179.38 1903 3323 74.62 2.12 3.39 59.91
11 Brazil 11135 11091 -0.40 3665 1678 -54.22 3.04 6.61 117.43
12 Japan 11863 10469 -11.75 1770 1462 -17.40 6.70 7.16 6.87
13 United States of America 8658 10320 19.20 1230 1208 -1.79 7.04 8.54 21.31
14 Nigeria 3298 8172 147.79 2199 4195 90.77 1.50 1.95 30.00
15 Nepal 4216 5551 31.67 1560 1459 -6.47 2.70 3.80 40.74
16 Sri Lanka 2860 5121 79.06 832 1066 28.12 3.44 4.80 39.53
17 Egypt 6000 4804 -19.93 659 499 -24.28 9.10 9.62 5.71
18 Republic of Korea 7197 4713 -34.51 1072 726 -32.28 6.71 6.49 -3.28
19 Madagascar 2480 4228 70.48 1209 1675 38.54 2.05 2.52 22.93
20 Democratic People’s Republic of Korea 1690 2113 25.03 535 470 -12.15 3.16 4.49 42.09

Tài liệu tham khảo

  1. https://www.fao.org/faostat/en/#data
  2. https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
  3. https://svgtopng.com/

Sơ kết

Trên đây là hướng dẫn tạo bảng xếp hạng trong R. Để 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/