A. Vẽ một chiều
1. Tải dữ liệu
library(readxl)
setwd("/Users/thuphan/Desktop/dataR")
dulieu <-read_excel("datatinh.xlsx")
head(dulieu)
## # A tibble: 6 × 4
## Tinh Gtri Tbinh ploai
## <chr> <dbl> <dbl> <dbl>
## 1 Đồng Nai 50 -15 0
## 2 Long An 45 -20 0
## 3 Bến Tre 40 -25 0
## 4 Hồ Chí Minh 35 -30 0
## 5 Hà Nội 75 10 1
## 6 Quãng Ngãi 85 20 1
2. Vẽ đồ thị chuẩn
library(ggplot2)
library(ggthemes)
library(scales)
ggplot(data=dulieu, aes(x=Tinh,y=Gtri, label=Gtri)) +
geom_bar(stat="identity", fill="#ab3b03") +
geom_label(color="#ab3b03", size=8) +
labs(x="", y="") +
coord_flip() -> m
m + theme_economist() + theme(axis.text = element_text(face="bold", color="#522915",size=14))

m + theme_wsj()

m + theme_calc()

m + theme_hc() + theme(axis.text = element_text(face="bold", color="#522915",size=14,angle=45))

3. Vẽ đồ thị bar trãi
library(forcats)
ggplot(data=dulieu, aes(x=fct_reorder(Tinh,Tbinh),y=Tbinh, label=abs(Tbinh))) +
geom_bar(stat="identity", aes(fill=ploai)) +
geom_label(aes(color=ploai), size=8) +
labs(x="", y="") + theme(legend.position="none")+
coord_flip() -> m2
m2 + scale_fill_gradient( low = "#ab3b03", high = "#56B1F7") +
scale_color_gradient( low = "#ab3b03", high = "#56B1F7") -> m2
m2 + theme_hc() + theme(legend.position="none")

ggplot(data=dulieu, aes(x=fct_reorder(Tinh,Tbinh),y=Tbinh, label=abs(Tbinh))) +
geom_point(stat='identity', aes(col=ploai), size=15) +
geom_text(color="white", size=8) +
scale_color_gradient( low = "#ab3b03", high = "#56B1F7")+
labs(x="", y="") + theme(legend.position="none")+
coord_flip() -> m3
m3 + theme_wsj() + theme(legend.position="none")

Đồ thị kẹo
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
ggplot(data=dulieu, aes(x=fct_reorder(Tinh,Tbinh),y=Tbinh, label=abs(Tbinh)))+
geom_lollipop(horizontal = FALSE, point.size=15, point.colour="green") +
geom_text(aes(color=ploai), size=8) -> m4
m4 + theme_wsj() + theme(legend.position="none") + coord_flip()

B. Vẽ đồ thị so sánh
dulieu2 <-read_excel("ssdta.xlsx")
head(dulieu2)
## # A tibble: 6 × 12
## Thu Tinh Nhom GDP LAB1 LAB2 CAP INV Nam Loai Chop Dinh
## <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2005 Đồng Nai Trước 119 1.26 2.15 8.85 5.76 2011 1 -10 10
## 2 2006 Đồng Tháp Trước 89 0.01 8.86 5.5 9.62 2012 1 -9 9
## 3 2007 Long An Trước 45 7.46 7.11 10.6 8.61 2013 1 -8 8
## 4 2008 An Giang Trước 120 7.28 2.45 10.2 8.38 2014 1 -7 7
## 5 2009 Hải Dương Trước 49 1.72 2 11.0 9.15 2015 1 -6 6
## 6 2010 Hải Phòng Trước 89 7.14 9.74 7.32 9.8 2016 1 -5 5
# Độ thị dạng dodge
p<-ggplot(dulieu2, aes(x=Tinh, y=GDP, fill=Nhom, label=GDP)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=paste(GDP, "%")), color=dulieu2$Loai,vjust= ifelse(dulieu2$Loai==1,-0.6, 1.4) , hjust= -0.3, size=5)+
labs(x="", y="")
p + theme_wsj() + theme(legend.position="none") + coord_flip()

# Đồ thị dạng stack
p2<-ggplot(dulieu2, aes(x=Tinh, y=GDP, fill=Nhom, label=GDP)) +
geom_bar(stat="identity", position="stack") +
geom_text(color="white", size=5, hjust= ifelse(dulieu2$Loai==1,-3.2, 5)) +
labs(x="", y="")
p2 + theme_wsj() + theme(legend.position="none") + coord_flip()

# Đồ thị dạng %
p3<-ggplot(dulieu2, aes(x=Tinh, y=GDP, fill=Nhom, label=GDP)) +
geom_bar(stat="identity", position="fill") +
geom_label(aes(color="red", size=5)) +
labs(x="", y="")
p3 + theme_wsj() + theme(legend.position="none") + coord_flip()

3. Vẽ biểu đồ tháp
brks <- seq(-15000000, 15000000, 5000000)
lbls = paste0(as.character(c(seq(15, 0, -5), seq(5, 15, 5))), "m")
p<-ggplot(dulieu2, aes(x=Tinh, y=Chop, fill=Nhom, label=Chop)) +
geom_bar(stat="identity", position="stack") +
geom_text(aes(label=paste(Chop, "%")), color=dulieu2$Loai,vjust= ifelse(dulieu2$Loai==1,-0.6, 1.4) , hjust= -0.3, size=5)+
scale_y_continuous(breaks = brks, label=lbls) +
labs(x="", y="")
p + theme_wsj() + theme(legend.position="none") + coord_flip()

library(ggpol)
ggplot(dulieu2, aes(x = Tinh, y = Chop, fill = Nhom)) +
geom_bar(stat = "identity") +
coord_flip() -> k
k + facet_share(~Nhom, dir = "h", scales = "free", reverse_num = TRUE)

k + facet_wrap(~Nhom, dir = "h", scales = "free")

ggplot(dulieu2, aes(x = factor(Nam), y = Dinh, fill = Nhom, label=abs(Dinh))) +
geom_bar(stat = "identity") +
geom_text(color="white",size=5, hjust=ifelse(dulieu2$Loai==1, 1.2, -0.7)) +
coord_flip() -> map
map

map + facet_share(~Nhom, dir = "h", scales = "free", reverse_num = TRUE) ->map2
map2 + theme_classic() + labs(x="", y="") + theme(legend.position="none") -> map3
map3
