### Hướng dẫn vẽ heatmap sử dụng package plot.matrix

### xử lý data về dạng matrix

rice_ready <- readRDS(file = "rice_ready.rds")

rice_ready -> rice_full_1 # gán vào object để tận dụng code template

matrix_rice <- rice_full_1[, c(1, 3, 4)] # chỉ lấy cột area, year, production

matrix_rice$area <- reorder(matrix_rice$area, matrix_rice$production)

library(dplyr)

matrix_rice <- matrix_rice |> dplyr::arrange(desc(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]

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

v <- matrix_2

## convert qua matrix
v <- as.matrix(v)

z <- v/1000000 # tính theo triệu tấn

# View(z)
# 
# summary(z)
# 
# z[which(is.na(z))] <- 0
# 
# heatmap(z)
# 
# heatmap(z[1:10, 1:10])

# https://jeromyanglim.blogspot.com/2010/05/abbreviations-of-r-commands-explained.html

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

nrow(z) -> a
a <- as.character(a)

library(Cairo)
Cairo::CairoPNG(
    width = 2000,
    height = 4000,
    file = "heatmap_128c.png",
    bg = "white",
    dpi = 200,
    units = "px"
)

library(plot.matrix)

oldpar <- par(no.readonly = TRUE)

par("cex.axis" = 0.5)

par(mar = c(4, 9, 6, 4)) # bottom, left, top, right

par(xpd = TRUE)

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

par(lheight = 1.15)

plot.matrix:::plot.matrix(z,
                          las = 1,
                          key = list(side = 4, font = 2),
                          col = c("#00ffff", hsv(0.1, seq(0.15, 1, length.out = 8), 1), "#ff685d", "#ff4e41", "#ff3122"),
                          na.col = "gray",
                          main = "",
                          axis.row = list(side = 2, font = 2, tick = FALSE),
                          axis.col = list(side = 1, font = 2),
                          xlab = "", ylab = "",
                          breaks = c(0, 0.0000000001, 1, 10, 30, 60, 90, 100, 130, 160, 200, 230, 260),
                          fmt.key = "%.0f" # số thập phân
)

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, # canh lề trái/giữa/phải
      col.main = "blue", 
      cex.main = 0.9, 
      line = 3.5)

# mysubtitle <- "Thứ tự của 128 quốc gia và vùng lãnh thổ được sắp xếp theo sản lượng sản xuất."

mysubtitle <- bquote(bold("Thứ tự của") ~ bold(.(a)) ~ bold("quốc gia và vùng lãnh thổ được sắp xếp theo sản lượng sản xuất."))

mtext(side = 3, 
      line = 1.8, 
      adj = 0, 
      cex = 0.8, 
      text = mysubtitle, 
      col = "red")

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

legend(x = "bottom",
       y = NULL,
       legend = c("Không có dữ liệu", "Không sản xuất"),
       fill = c("grey", "#00ffff"),
       box.lty = 0,
       horiz = TRUE,
       cex = 0.8,
       x.intersp = 1,
       y.intersp = 2,
       inset = -0.045,
       bg = "transparent")

##################### 
par(new = TRUE)

par(yaxt = "none")

plot.matrix:::plot.matrix(z,
                          las = 1,
                          key = list(side = 4, font = 2),
                          col = c("#00ffff", hsv(0.1, seq(0.15, 1, length.out = 8), 1), "#ff685d", "#ff4e41", "#ff3122"),
                          na.col = "gray",
                          main = "",
                          axis.row = list(side = 2, font = 2, tick = FALSE),
                          axis.col = list(side = 3, font = 2),
                          xlab = "", ylab = "",
                          breaks = c(0, 0.0000000001, 1, 10, 30, 60, 90, 100, 130, 160, 200, 230, 260),
                          fmt.key = "%.0f" # số thập phân
)


library(png) 
library(grid) 
logor <- readPNG("logo-blue.png")
grid.raster(logor, x = 0.1, y = 0.97, width = 0.1)

par(oldpar)

dev.off()



