Load các gói cần thiết
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggrepel)
attach(datamoi)
datamoi$nhan <-round(datamoi$X2,2)
datamoi$solieu <-round(datamoi$X,0)
Vẽ đồ thì 50-50 với dữ liệu CPI
ggplot(datamoi, aes(x=tentinh, y=nhan)) +
geom_bar(stat='identity', aes(fill=ploai), width=.5) +
geom_text(data = datamoi %>% filter(nhan > 0), aes(label = nhan), hjust = -0.3, color = "#00ba38", size = 5,,fontface="bold") +
geom_text(data = datamoi %>% filter(nhan <= 0), aes(label = nhan), hjust = 1.2, color = "#f8766d", size = 5,,fontface="bold") +
scale_fill_manual(name="Chú thích:",
labels = c("Trên trung bình", "Dưới trung bình"),
values = c("Duoi"="#00ba38", "Tren"="#f8766d")) +
labs(subtitle="PCI trung bình giai đoạn 2010-2018",
x=NULL,
y=NULL,
title= "Biểu đồ Diverging Bars" )+
ylim(-5.5,7.1)+
geom_rect(aes(xmin = 0.5, xmax = Inf, ymin = 7, ymax = 7.1), fill = "#efefe3")+
geom_text_repel(data =filter(datamoi,nhan > 0) , aes(x = tentinh, y = 7, label = solieu),direction = c("both", "y", "x"),nudge_y = 1,colour="#00ba38",size=5,fontface="bold") +
geom_text_repel(data = filter(datamoi,nhan <= 0), aes(x = tentinh, y = 7, label = solieu),direction = c("both", "y", "x"),nudge_y = 1,colour="#f8766d",size=5,fontface="bold") +
coord_flip()+
theme(axis.text.y=element_text(size=13,color="#002feb"))+
theme(legend.position = "none")
