Kết nối dữ liệu
setwd("d:/DATA2021/ThuySan.VECM/treemap")
library(readxl)
dulieu <-read_excel("xuatkhau.tb.xlsx")
head(dulieu)
## # A tibble: 6 x 4
## Country TENQG XK3 XK
## <chr> <chr> <dbl> <dbl>
## 1 AUS Úc 47.5 47.5
## 2 CAN Canada 49.6 49.6
## 3 CHN Trung Qu<U+1ED1>c 162. 162.
## 4 DEU Ð<U+1EE9>c 50.6 50.6
## 5 GBR Anh 51.6 51.6
## 6 JPN Nh<U+1EAD>t b<U+1EA3>n 297. 297.
Vẽ đồ thị map
library(treemapify)
## Warning: package 'treemapify' was built under R version 4.0.3
library(ggplot2)
ggplot(data=dulieu, aes(area = XK, fill = -XK)) +
geom_treemap() +
geom_treemap_text(aes(label = TENQG), colour = "white", place = "topleft", fontface = "italic") +
geom_treemap_text(aes(label = XK),fontface="bold", place="center")

Đưa đồ thị 16:9
ggplot(data=dulieu, aes(area = XK, fill = -XK)) +
geom_treemap(linetype = "dashed", size=3, color="white") +
geom_treemap_text(aes(label = TENQG), colour = "white", place = "topleft", fontface = "italic", size=35) +
geom_treemap_text(aes(label = XK),colour = "yellow", fontface="bold", place="center", size=50) +
theme(legend.position="none")

Vẽ đồ thị bar
library(ggthemes)
ggplot(data=dulieu, aes(x=TENQG, y=XK, label=XK)) +
geom_bar(stat="identity", fill="#DC241f") +
geom_label(color="#DC241f") +
labs(x="", y="") +
theme_economist() +
coord_flip()

Thêm tô màu
library("RColorBrewer")
dulieu$XK10 = findInterval(dulieu$XK, sort(dulieu$XK))
head(dulieu)
## # A tibble: 6 x 5
## Country TENQG XK3 XK XK10
## <chr> <chr> <dbl> <dbl> <int>
## 1 AUS Úc 47.5 47.5 2
## 2 CAN Canada 49.6 49.6 3
## 3 CHN Trung Qu<U+1ED1>c 162. 162. 8
## 4 DEU Ð<U+1EE9>c 50.6 50.6 5
## 5 GBR Anh 51.6 51.6 6
## 6 JPN Nh<U+1EAD>t b<U+1EA3>n 297. 297. 9
mausac <-brewer.pal(n = 9, name = "Greens")
msac = colorRampPalette(mausac)
ggplot(data=dulieu, aes(area = XK)) +
geom_treemap(aes(fill=factor(-XK10)),linetype = "dashed", size=3, color="white") +
geom_treemap_text(aes(label = TENQG), colour = "white", place = "topleft", fontface = "italic", size=35) +
geom_treemap_text(aes(label = XK),colour = "yellow", fontface="bold", place="center", size=50) +
theme(legend.position="none") + scale_fill_manual(values=c("#00235c", "#073073", "#0e3e8c", "#1d54ad", "#2862bf", "#3a73cf", "#558ce6", "#7aa7f0", "#a3c3f7", "#c6d8f5"))

#scale_fill_brewer(palette="Set3")