Vẽ đồ thị PCI 13 tỉnh miền tây

Dạng đồ thị barplot nằm ngang


1. Lấy dữ liệu về

library(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

2. Tạo dữ liệu mới

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

3. Vẽ đồ thị barplot nằm ngang

library(hrbrthemes)
my_colors<-c("#8C3F4D")
my_font <-"Roboto Condensed"
library(ggplot2)
  ggplot(data=datamoi,aes(x=tentinh, y=so)) +
  geom_col(fill = my_colors, color = my_colors, width = 0.8) +
  
  geom_text(aes(label = so), hjust = 1.1, color = "white", size = 5.5, family = my_font) + 
  coord_flip() +
  theme_ft_rc() + 
  theme(panel.grid = element_blank()) + 
  theme(axis.text.x = element_blank()) + 
  theme(axis.text.y = element_text(color = "white", size = 16, family = my_font)) + 
  theme(plot.title = element_text(size = 28)) + 
  theme(plot.subtitle = element_text(family = my_font, size = 16, color = "grey80")) + 
  theme(plot.caption = element_text(family = my_font, size = 13, face = "italic")) + 
  scale_y_discrete(expand = c(0.01, 0)) + 
  theme(plot.margin = unit(c(1.2, 1.2, 1.2, 1.2), "cm")) + 
  labs(x = NULL, y = NULL, 
       title = "Đồ thị 13 tỉnh miền tây Nam bộ", 
       subtitle = "Dạng đồ thị Barplot nằm ngang", 
       caption = "Data Source: Thongke.club")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database