Heatmap là đồ thị thể hiện độ lớn của dữ liệu thông qua màu sắc. Dữ liệu vẽ heatmap thường ở dạng matrix. Để phóng lớn hình ảnh, bạn right-click vào hình và chọn Open in new tab nhé.

https://cran.r-project.org/web/packages/plot.matrix/vignettes/plot.matrix.html

rice_full_1 <- readRDS("rice_full_1.rds")

names(rice_full_1)[4] <- "production"
names(rice_full_1)[5] <- "area_harvested"

str(rice_full_1)
## 'data.frame':    7113 obs. of  5 variables:
##  $ area          : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ item          : chr  "Rice" "Rice" "Rice" "Rice" ...
##  $ year          : int  2021 2020 2019 2018 2017 2016 2015 2014 2013 2012 ...
##  $ production    : num  458572 439549 382500 352177 338420 ...
##  $ area_harvested: num  152499 147555 127530 117539 109452 ...

Link dataset gốc

https://docs.google.com/spreadsheets/d/1MoR1_qRLQ1nhUrV0-2Y5YzPa8lAfebHiHp-ieBRCBqw/edit?usp=sharing

Link dataset transform

https://docs.google.com/spreadsheets/d/1v_t1-gRQpoO6S7QJEi0v9ds8TsFBvmd8jMU5HWHYqLs/edit?usp=sharing

library(plot.matrix)

z <- as.matrix(table(rice_full_1$area, rice_full_1$year))

par(mar = c(3, 9, 6, 2))

par(mgp = c(0, 0.7, 0))

par("cex.axis" = 0.5)
par(xpd = TRUE) # đưa legend outside plot

plot.matrix:::plot.matrix(z,
                          las = 1, 
                          key = NULL,
                          col = c("gray92", "#fcc203"),
                          main = "",
                          axis.row = list(side = 2, font = 2, tick = FALSE),
                          axis.col = list(side = 1, font = 2)
                          )

title(main = "Các quốc gia sản xuất lúa gạo trên thế giới (1961–2021) | Nguồn: FAOSTAT | Đồ họa: tuhocr.com",
      adj = 0, col.main = "blue", cex.main = 0.9, line = 3.5)

mysubtitle <- expression(italic("Sắp xếp theo alphabet"))
mtext(side = 3, line = 1.8, adj = 0, cex = 0.8, mysubtitle, col = "red")

legend("topright",
       legend = c("Có dữ liệu sản xuất gạo", "Không có dữ liệu"),
       fill = c("#fcc203", "gray92"),
       # lty = c(1, 2), cex = 1,
       # pch = c(NA, 21),
       # lwd = 2,
       x.intersp = 1,
       y.intersp = 2,
       xjust = 2,
       yjust = 0,
       inset = -0.045,
       box.lty = 0,
       horiz = TRUE, 
       cex = 0.8,
       bg = "transparent")

par(new = TRUE)

par(yaxt = "none")

par(mgp = c(0, 0.7, 0))

plot.matrix:::plot.matrix(z,
                          las = 1,
                          key = NULL,
                          col = c("gray92", "#fcc203"),
                          main = "",
                          # axis.row = list(side = 2, font = 2),
                          axis.col = list(side = 3, font = 2)
                          )

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.12, y = 0.97, width = 0.1)

Khi ta đưa giá trị vào heatmap sẽ trở nên nhiều thông tin hơn.

####### sắp xếp big matrix

matrix_rice <- rice_full_1[, c(1, 3, 4)]

#spread

library(dplyr)

matrix_rice <- matrix_rice |> dplyr::arrange(area, year)

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

colnames(matrix_1)[2:62] <- unique(matrix_rice$year)

matrix_2 <- matrix_1[, 2:62]

rownames(matrix_2) <- matrix_1[, 1]

head(matrix_2)
##               1961   1962   1963   1964   1965   1966   1967   1968   1969
## Afghanistan 319000 319000 319000 380000 380000 337000 396000 402000 407000
## Albania       4603   5683   9135   8173  10225  10524  11254  12807  14276
## Algeria       9512   8000   6659   5014   5530   5550   4558   6110   6711
## Angola       29000  23000  25000  26000  32000  30000  33000  15148  15945
## Argentina   149000 182300 178200 190000 267600 165300 217000 282900 345000
## Australia   118000 139000 140000 146000 158000 187000 221000 228000 267000
##               1970   1971   1972   1973   1974   1975   1976   1977   1978
## Afghanistan 366000 350000 400000 420000 420000 435000 448000 400000 428000
## Albania      14924  10760  12000  15168  12000  13500  14000  14400  14800
## Algeria       2166   1568   2218   2447   2520   1600   1279   1409    291
## Angola       29950  27791  20347  29051  16779  20000  20000  20000  15000
## Argentina   407000 288000 294000 260000 316000 351000 309000 320000 310000
## Australia   250000 301000 248000 309000 409000 387000 417000 530000 489697
##               1979   1980   1981   1982   1983   1984   1985   1986   1987
## Afghanistan 439000 415000 390000 364000 350000 334000 317000 336000 324000
## Albania      15520  13000  13900  11900  13000  12600  12000  11000  10600
## Algeria       1000   1200   1200   1200   1200   1200   1200   1240   1300
## Angola       13000  11000  10000  10000   8000   8000   5000   5000   2000
## Argentina   312000 266000 286300 437200 337100 480400 400000 438600 371000
## Australia   692151 613163 760000 857000 520000 634000 864000 687000 549000
##               1988   1989   1990   1991       1992      1993       1994
## Afghanistan 343000 320000 333000 335000  300000.00 300000.00  342000.00
## Albania       8830   8450   7000   2283     960.00    585.00       0.00
## Algeria       1480   1480   1540   1500    1410.14   1289.67    1243.63
## Angola        2000   3000   3000   4000    4000.00   4000.00   21000.00
## Argentina   383400 490000 428100 347600  732700.00 608300.00  607600.00
## Australia   761000 805000 924000 787000 1122000.00 955000.00 1082000.00
##                   1995      1996    1997       1998       1999       2000
## Afghanistan  390000.00 340000.00  400000  450000.00  280000.00  260000.00
## Albania           0.00      0.00       0       0.00       0.00       0.00
## Algeria        1158.48   1072.89     300     492.49     456.36     437.39
## Angola        19000.00  14000.00   12000   10000.00    7402.00    5776.00
## Argentina    926200.00 986000.00 1205140 1011135.00 1658200.00  903630.00
## Australia   1137000.00 951200.00 1388000 1330900.00 1389800.00 1100700.00
##                   2001       2002      2003       2004      2005       2006
## Afghanistan  242000.00  388000.00 434000.00  463000.00 485000.00  540000.00
## Albania           0.00       0.00      0.00       0.00      0.00       0.00
## Algeria         418.68     400.24    425.06     413.92    390.19     359.98
## Angola         5335.00    4890.00  10831.00   13000.00   8650.00    3831.00
## Argentina    873183.00  709295.00 717630.00 1060083.00 956253.00 1193492.00
## Australia   1643403.00 1192202.00 438111.00  553114.00 338885.00 1002654.00
##                   2007       2008       2009       2010       2011    2012
## Afghanistan  552000.00  612000.00  645000.00  672000.00  672000.00  500000
## Albania           0.00       0.00       0.00       0.00       0.00       0
## Algeria         348.33     334.32     320.23     311.54     320.01     300
## Angola         4635.00    8414.00   14291.00   17697.00   23209.00   21492
## Argentina   1080070.00 1245800.00 1334155.00 1243259.00 1748075.00 1567971
## Australia    162806.00   17614.00   60868.00  196684.00  723283.00  918733
##                2013    2014      2015       2016       2017       2018
## Afghanistan  512094  537000  410000.0  356565.00  338420.00  352177.00
## Albania           0       0       0.0       0.00       0.00       0.00
## Algeria         320     320     280.6     306.87     316.14     315.34
## Angola        37608   42288   45322.0   24576.00    9426.00    9699.00
## Argentina   1563450 1581810 1558100.0 1404980.00 1328340.00 1367968.00
## Australia   1161115  819276  690390.0  273942.00  807304.46  635113.00
##                   2019       2020       2021
## Afghanistan  382500.00  439549.00  458571.64
## Albania           0.00       0.00       0.00
## Algeria         312.78     314.75     314.29
## Angola        10102.00   10567.00   10514.00
## Argentina   1189866.00 1222910.00 1453187.00
## Australia     66835.34   50226.33  422977.54

Vẽ heatmap

Link dataset heatmap

https://docs.google.com/spreadsheets/d/1CG0z3FPQKzfeSt2_apIMoI54_y4N0ivsGpQlZdjbiMo/edit?usp=sharing

library(plot.matrix)
library(RColorBrewer)
options(scipen = 6, digits = 2)
v <- matrix_2
## convert qua matrix
v <- as.matrix(v)

z <- v/1000000

par(mar = c(3, 9, 6, 4))

par(mgp = c(0, 0.7, 0))

par("cex.axis" = 0.5)
par(xpd = TRUE) # đưa legend outside plot

plot.matrix:::plot.matrix(z,
                          las = 1, 
                          key = list(side = 4, font = 2),
                          col = brewer.pal(n = 9, name = "YlOrRd"),
                          na.col = "white",
                          main = "",
                          axis.row = list(side = 2, font = 2, tick = FALSE),
                          axis.col = list(side = 1, font = 2),
                          xlab = "", ylab = "",
                          # breaks = 12
                          )

title(main = "Các quốc gia sản xuất lúa gạo trên thế giới (1961–2021) | Nguồn: FAOSTAT | Đồ họa: tuhocr.com",
      adj = 0, col.main = "blue", cex.main = 0.9, line = 3.5)

mysubtitle <- expression(italic("Sắp xếp theo alphabet"))
mtext(side = 3, line = 1.8, adj = 0, cex = 0.8, mysubtitle, col = "red")

legend("topright",
       legend = c("Không có dữ liệu"),
       fill = c("white"),
       # lty = c(1, 2), cex = 1,
       # pch = c(NA, 21),
       # lwd = 2,
       x.intersp = 1,
       y.intersp = 2,
       inset = -0.045,
       box.lty = 0,
       horiz = TRUE, 
       cex = 0.8,
       bg = "transparent")

par(new = TRUE)

par(yaxt = "none")

par(mgp = c(0, 0.7, 0))

plot.matrix:::plot.matrix(z,
                          las = 1,
                          key = list(side = 4, font = 2),
                          col = brewer.pal(n = 9, name = "YlOrRd"),
                          na.col = "white",
                          main = "",
                          # axis.row = list(side = 2, font = 2),
                          axis.col = list(side = 3, font = 2),
                          xlab = "", ylab = "",
                          )

par(lheight = 1.15) # chỉnh khoảng cách giữa hai dòng text

text(x = 66, y = 120, 
     substitute(paste(bold("Đơn vị: \ntriệu tấn"))), cex = 0.7,
     col = "red")

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.12, y = 0.97, width = 0.1)

Sơ kết

Trên đây là hướng dẫn vẽ heatmap 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/