# 99. Rchart --------------------------------------------------------------
library(ggplot2)
library(readxl)
library(dplyr)
library(reshape2)
library(extrafont)
my_font <- "OfficinaSansITC"
df1 <- read_excel('D:\\VUDT\\8.TRAINING\\Chart\\world_bank\\Popular Indicators.xlsx',sheet = 1)
# 99.1 GPD, Pop -----------------------------------------------------------
library(ggplot2)
library(dplyr)
library(reshape2)
library(ggrepel)
library(ggthemes)
library(grid)
library(cowplot)
library(extrafont)
my_font <- "OfficinaSansITC"
df1 <- read_excel('D:\\VUDT\\8.TRAINING\\Chart\\world_bank\\Popular Indicators.xlsx',sheet = 1)
# df1 %>% select(`Series Name`) %>% distinct() %>% View()
df <-
df1 %>%
filter(`Series Name` %in% c('Population, total','GDP (current US$)','Inflation, GDP deflator (annual %)')) %>%
select(`Country Name`,`Series Name`,`2017 [YR2017]`,`2016 [YR2016]`) %>%
filter(`Country Name` %in% c('Brunei Darussalam','Cambodia','Indonesia','Lao PDR','Malaysia','Myanmar',
'Philippines','Singapore','Thailand','Timor-Leste','Vietnam'))
df <-
df %>%
select(`Country Name`,`Series Name`,`2017 [YR2017]`) %>%
dcast(`Country Name` ~ `Series Name`)
names(df)[1] <- 'Country'
names(df)[2] <- 'GDP'
names(df)[3] <- 'INF'
names(df)[4] <- 'POP'
df$GDP <- as.numeric(df$GDP)
df$POP <- as.numeric(df$POP)
df$INF <- as.numeric(df$INF)
df <-
df %>%
mutate(GDP = GDP/1e9,
POP = POP/1e6)
Label.Point <- df$Country
# Hàm hồi quy Số lượng sales và số tiền giải ngân mới....
lm.fit <- lm(data = df,
GDP ~ POP)
lm.fit.square <- lm.fit %>% summary()
# lm.fit.square$r.squared
lm.fit.square$coefficients %>% as.data.frame() -> a
df %>%
ggplot(aes(x = POP,y = GDP)) +
geom_point(aes(size = GDP/POP), colour = '#23576E') +
geom_smooth(aes(fill='red'),
formula = y ~ log(x),
method = 'lm',
se = FALSE,
color = 'red') +
geom_text_repel(aes(label = df$Country),
color = 'gray20',
size = 3,
min.segment.length = 1,
segment.colour = 'red',
family = 'serif',
box.padding = unit(1, 'lines'),
data = subset(df, df$Country %in% Label.Point)) +
geom_hline(yintercept = mean(df$GDP), linetype="dashed", size=.1) +
# theme_economist() +
# theme_bw() +
theme(plot.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(panel.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(legend.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(text = element_text(family = 'serif'),
# title = element_text(size = 14),
plot.title = element_text(hjust = 0.5,face = 'bold'),
axis.text.x = element_text(size = 9) ,
axis.text.y = element_text(size = 9) ,
legend.text = element_text(size = 7),
legend.title = element_text(size = 7),
legend.position = 'top') +
scale_fill_manual(name='', values=c('red'),
# labels=c('R_square=52%')
labels = paste0('R_square =',round(lm.fit.square$r.squared,2)*100,'%')) +
# scale_shape_discrete(name='Amount disbursement') +
scale_colour_gradient() +
# guides(size = FALSE) +
geom_curve(aes(x = 200, y = 190, xend = 200, yend = 250),arrow = arrow(length = unit(0.01, 'npc'))) +
annotate('text', x = 200, y = 170, size = 3, family = 'serif', label = "GDP b\u00ECnh qu\u00E2n khu v\u1EF1c\n \u0110\u00F4ng Nam \u00C1")+
labs(x = ' \r\n\r\nT\u00D4\u0309NG D\u00C2N S\u00D4\u0301 (TRI\u00CA\u0323U NG\u01AF\u01A0\u0300I)',
y = 'T\u00D4\u0309NG SA\u0309N PH\u00C2\u0309M QU\u00D4\u0301C N\u00D4\u0323I (TY\u0309 USD)',
size ="GDP tr\u00EAn \u0111\u00E2\u0300u ng\u01B0\u01A1\u0300i (Nghi\u0300n USD)",
title = '',
caption = "Ngu\u00F4\u0300n: https://databank.worldbank.org") -> A1
# A2 <- add_sub(A1,'Ta\u0301c gia\u0309: Vu\u0303 \u0110\u0103\u0323ng\nMail: tuanvu.dang257@gmail.com',
# x = 0,
# hjust = 0,
# fontface = 'plain',
# fontfamily = 'serif',
# size= 10)
A2 <- A1
ggdraw(A2)# 99.2 GDP - Đông Nam Á --------------------------------------------------
df_2016 <-
df1 %>%
filter(`Series Name` %in% c('Population, total','GDP (current US$)')) %>%
select(`Country Name`,`Series Name`,`2016 [YR2016]`) %>%
filter(`Country Name` %in% c('Brunei Darussalam','Cambodia','Indonesia','Lao PDR','Malaysia','Myanmar',
'Philippines','Singapore','Thailand','Timor-Leste','Vietnam'))
df_2016 <- df_2016 %>% dcast(`Country Name` ~ `Series Name`)
df_2016$`GDP (current US$)` <- as.numeric(df_2016$`GDP (current US$)`)
df_2016$`Population, total` <- as.numeric(df_2016$`Population, total`)
df_2016 <- df_2016 %>% mutate(`2016 [YR2016]` = (`GDP (current US$)`/`Population, total`))
df_2017 <-
df1 %>%
filter(`Series Name` %in% c('Population, total','GDP (current US$)')) %>%
select(`Country Name`,`Series Name`,`2017 [YR2017]`) %>%
filter(`Country Name` %in% c('Brunei Darussalam','Cambodia','Indonesia','Lao PDR','Malaysia','Myanmar',
'Philippines','Singapore','Thailand','Timor-Leste','Vietnam'))
df_2017 <- df_2017 %>% dcast(`Country Name` ~ `Series Name`)
df_2017$`GDP (current US$)` <- as.numeric(df_2017$`GDP (current US$)`)
df_2017$`Population, total` <- as.numeric(df_2017$`Population, total`)
df_2017 <- df_2017 %>% mutate(`2017 [YR2017]` = (`GDP (current US$)`/`Population, total`))
df <- df_2017 %>% left_join(df_2016, by = 'Country Name') %>% select(`Country Name`,`2017 [YR2017]`,`2016 [YR2016]`)
df$`2017 [YR2017]` <- as.numeric(df$`2017 [YR2017]`)
df$`2016 [YR2016]` <- as.numeric(df$`2016 [YR2016]`)
# df %>% arrange(`2017 [YR2017]`)
# df %>% View()
left_label <- paste(df$`Country Name`, round(df$`2016 [YR2016]`,0),sep=", ")
right_label <- paste(df$`Country Name`, round(df$`2017 [YR2017]`,0),sep=", ")
df$class <- ifelse((df$`2017 [YR2017]` > median(df$`2017 [YR2017]`)), "green", "red")
left_label <- c("Brunei Darussalam, 26939","","Indonesia, 3569","" ,
"Malaysia, 9515","","","Singapore, 55243",
"Thailand, 5979","","Vietnam, 2171" )
right_label <- c( "Brunei Darussalam, 28291","=","Indonesia, 3846","",
"Malaysia, 9952","","","Singapore, 57714",
"Thailand, 6595","","Vietnam, 2342" )
# Plot
p <- ggplot(df) +
geom_segment(aes(x=2016, xend=2017, y=`2016 [YR2016]`, yend=`2017 [YR2017]`, col=class), size=.75, show.legend=F) +
geom_vline(xintercept=2016, linetype="dashed", size=.1) +
geom_vline(xintercept=2017, linetype="dashed", size=.1) +
scale_color_manual(labels = c("Up", "Down"),
values = c("green"="#00ba38", "red"="#f8766d")) + # color of lines
labs(x="",
# y="D\u01B0 n\u01A1\u0323 cu\u00F4\u0301i ki\u0300",
y = "",
title = 'T\u00D4\u0309NG SA\u0309N PH\u00C2\u0309M QU\u00D4\u0301C N\u00D4\u0323I TR\u00CAN \u0110\u00C2\u0300U NG\u01AF\u01A0\u0300I\n KHU V\u01AF\u0323C \u0110\u00D4NG NAM A\u0301 (NGHI\u0300N USD)') + # Axis labels
xlim(2015.5,2017.5) + ylim(0,(1.1*(max(df$`2016 [YR2016]`, df$`2017 [YR2017]`)))) # X and Y axis limits
# Add texts
p <- p + geom_text(label=left_label, y=df$`2016 [YR2016]`, x=rep(2016, NROW(df)), hjust=1.1, size=3.5, family = 'serif')
p <- p + geom_text(label=right_label, y=df$`2017 [YR2017]`, x=rep(2017, NROW(df)), hjust=-0.1, size=3.5, family = 'serif')
p <- p + geom_text(label="2016", x=2016, y=1.1*(max(df$`2016 [YR2016]`, df$`2017 [YR2017]`)), hjust=1.2, size=5, family = 'serif') # title
p <- p + geom_text(label="2017", x=2017, y=1.1*(max(df$`2016 [YR2016]`, df$`2017 [YR2017]`)), hjust=-0.1, size=5, family = 'serif') # title
# Minify theme
p1 <-
p +
theme(panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(1,2,1,2), "cm"),
axis.title = element_text(family = my_font,size = 16),
text = element_text(family = 'serif')) +
theme(title = element_text(size = 16)) +
theme(plot.title = element_text(hjust = 0.5,face = 'bold')) +
theme(plot.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(panel.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(legend.background = element_rect(fill = "#EFF2F4", color = NA))
p1# library("gridExtra")
# grid.arrange(ggdraw(A2),p1,
# ncol = 2, nrow = 1)