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
