library(ggplot2)
library(ggalt)
## Registered S3 methods overwritten by 'ggalt':
## method from
## grid.draw.absoluteGrob ggplot2
## grobHeight.absoluteGrob ggplot2
## grobWidth.absoluteGrob ggplot2
## grobX.absoluteGrob ggplot2
## grobY.absoluteGrob ggplot2
dulieu <- read.csv("http://solieu.vip/csv/data2018.csv")
attach(dulieu)
head(dulieu)
## ï..STT YEAR PROVINE CODE LAB LnLAB PCI X_MARKET X_DATDAI
## 1 1 2010 Long An 1 854.4 6.750399 55.34 7.19 6.984733
## 2 2 2010 Tiá»\201n Giang 2 1011.5 6.919190 53.58 6.92 7.480912
## 3 3 2010 Bến Tre 3 792.6 6.675319 57.14 7.59 7.297698
## 4 4 2010 Trà Vinh 4 584.1 6.370072 59.54 7.37 8.654261
## 5 5 2010 Vĩnh Long 5 612.9 6.418202 55.94 7.04 6.617867
## 6 6 2010 Ä\220ồng Tháp 6 988.6 6.896290 62.08 7.09 7.365447
## X_MINHBACH X_TIMES X_CPHI09 X_NANGDONG X_HTCTY X_LDONG X_PHAPLY SL FDI
## 1 6.50 7.07 6.98 5.55 4.90 5.58 4.59 38 628.8
## 2 6.19 5.38 7.04 6.27 4.53 5.37 4.40 15 160.8
## 3 5.84 7.18 8.22 6.68 3.88 4.90 5.55 9 38.7
## 4 6.07 7.04 8.53 7.75 3.58 5.36 5.19 7 54.2
## 5 6.65 7.08 7.53 6.00 4.19 5.40 5.43 4 4.8
## 6 6.26 8.08 7.57 7.38 6.03 5.13 7.17 0 0.0
## LnFDI Cno SHIP LnSHIP Port Lab15_Edu LabEdu
## 1 6.443813 116.4 15301.5 9.635706 0 79,772 79.77
## 2 5.080161 115.1 9785.3 9.188637 1 87,514 87.51
## 3 3.655840 108.1 3216.6 8.076080 0 69,100 69.10
## 4 3.992681 143.1 3429.3 8.140111 0 45,886 45.89
## 5 1.568616 109.9 4926.2 8.502323 1 45,638 45.64
## 6 0.000000 107.4 3019.9 8.012979 1 57,000 57.00
datamoi <-aggregate(PCI,by=list(PROVINE), FUN=mean)
names(datamoi) <-c("Nhom","X")
datamoi$X2 <-datamoi$X -mean(datamoi$X)
datamoi$ploai <-ifelse(datamoi$X2 <0,"Tren","Duoi")
datamoi <-datamoi[order(datamoi$X),]
datamoi$tentinh <-factor(datamoi$Nhom, levels=datamoi$Nhom)
datamoi$nhan <-round(datamoi$X2,2)
datamoi$so <-round(datamoi$X,2)
attach(datamoi)
head(datamoi)
## Nhom X X2 ploai tentinh nhan so
## 5 CÃ Mau 52.36111 -4.6485470 Tren CÃ Mau -4.65 52.36
## 11 Tiá»\201n Giang 54.57222 -2.4374359 Tren Tiá»\201n Giang -2.44 54.57
## 3 Bạc Liêu 56.08556 -0.9241026 Tren Bạc Liêu -0.92 56.09
## 7 Háºu Giang 56.51444 -0.4952137 Tren Háºu Giang -0.50 56.51
## 2 An Giang 56.52889 -0.4807692 Tren An Giang -0.48 56.53
## 6 Cần Thơ 56.62667 -0.3829915 Tren Cần Thơ -0.38 56.63
Lấy X làm dữ liệu năm 2019, giờ tạo ra thêm dữ liệu năm 2020
lech <-c(10, 14, 19, 21, 12, 17, 3, 5, 11, 17,20, 9, 13)
datamoi <- data.frame(datamoi, lech)
datamoi$solech <-datamoi$X + lech
head(datamoi)
## Nhom X X2 ploai tentinh nhan so lech
## 5 CÃ Mau 52.36111 -4.6485470 Tren CÃ Mau -4.65 52.36 10
## 11 Tiá»\201n Giang 54.57222 -2.4374359 Tren Tiá»\201n Giang -2.44 54.57 14
## 3 Bạc Liêu 56.08556 -0.9241026 Tren Bạc Liêu -0.92 56.09 19
## 7 Háºu Giang 56.51444 -0.4952137 Tren Háºu Giang -0.50 56.51 21
## 2 An Giang 56.52889 -0.4807692 Tren An Giang -0.48 56.53 12
## 6 Cần Thơ 56.62667 -0.3829915 Tren Cần Thơ -0.38 56.63 17
## solech
## 5 62.36111
## 11 68.57222
## 3 75.08556
## 7 77.51444
## 2 68.52889
## 6 73.62667
Giờ solech là dữ liệu năm 2020 ## 3. Vẽ đồ thị Dumbbel
gg <-ggplot(datamoi, aes(x=so, xend=solech, y=tentinh, group=tentinh)) +
geom_dumbbell(color="#32a8a8",size=1.75,colour_x = "#bf244b", colour_xend = "#1a2d8f", size_x = 4,size_xend = 5) +
labs(x=NULL,
y=NULL,
title="Dumbbell Chart",
subtitle="PCI Index: 2019 vs 2020",
caption="Source: https://thongke.club")
#scale_x_continuous(label=percent) +
gg
gg2 <-gg + theme(plot.title = element_text(hjust=0.5, face="bold"),
plot.background=element_rect(fill="#f7f7f7"),
panel.background=element_rect(fill="#f7f7f7"),
panel.grid.minor=element_blank(),
panel.grid.major.y=element_blank(),
panel.grid.major.x=element_line(),
axis.ticks=element_blank(),
legend.position="top",
panel.border=element_blank())
gg2
datachuan <-datamoi[13,]
head(datachuan)
## Nhom X X2 ploai tentinh nhan so lech solech
## 1 Ä\220ồng Tháp 62.14333 5.133675 Duoi Ä\220ồng Tháp 5.13 62.14 13 75.14333
datachuan <-data.frame(datachuan)
gg3 <- gg2 +
geom_rect(aes(xmin=90, xmax=94, ymin=-Inf, ymax=Inf), fill="#707382") +
scale_y_discrete(expand = c(0.12,0)) +
geom_text(data=datachuan, aes(x=92,y=tentinh,label="Lech"), vjust=-2, col="red")+
geom_text(data=datachuan, aes(x=solech, y=tentinh, label=" Nam 2020"), vjust= -2) +
geom_text(data=datachuan, aes(x=X, y=tentinh, label="Nam 2019"), vjust= -2)
gg3
gg3 +
geom_text( aes(x=92,y=tentinh,label=lech)) +
geom_text( aes(x=X, y=tentinh, label=round(X,2)), hjust= 1.25)+
geom_text( aes(x=solech, y = tentinh, label=round(solech,2)), hjust = -0.5)
Ghi chú: Đây là đồ thị để so sánh 2 cột mốc rất hay và trực quan.