vn_data_ok <- read.csv("C:/Users/ufm_t/OneDrive/Desktop/tuhocR2/GDP_DS/vn_data_ok.csv")
library(ggplot2)
library(extrafont)
## Registering fonts with R
library(ggtext)
## Warning: package 'ggtext' was built under R version 4.3.2
library(grid)
library(png)
scale_truc_y_second <- 45
limit_truc_y_first <- c(0, 120)
ggplot(vn_data_ok, aes(x = year, y = SP.POP.TOTL/1000000)) +
geom_line(aes(y = SP.POP.TOTL/1000000, color = "Dân số (triệu người)", linetype = "Dân số (triệu người)"),
linewidth = 1) +
geom_line(aes(y = NY.GDP.PCAP.CD/scale_truc_y_second,
color = "GDP bình quân (USD/người)",
linetype = "GDP bình quân (USD/người)"),
linewidth = 1) +
scale_x_continuous(expand = c(0, 0),
limits = c(1955, 2025),
breaks = c(1960, 1970, 1980, 1985, 1990, 2000, 2010, 2022)) +
scale_y_continuous(
### customize first y axis
expand = c(0, 0),
limits = limit_truc_y_first, # set limit chung cho 2 trục
breaks = round(c(limit_truc_y_first[1],
na.omit(vn_data_ok$SP.POP.TOTL)[1]/1000000,
40,
60,
80,
na.omit(vn_data_ok$SP.POP.TOTL)[length(na.omit(vn_data_ok$SP.POP.TOTL))]/1000000,
110,
limit_truc_y_first[2]), digits = 0),
sec.axis = sec_axis(~ . * scale_truc_y_second, name = "GDP bình quân đầu người (USD/người)",
breaks = round(c(limit_truc_y_first[1] * scale_truc_y_second,
na.omit(vn_data_ok$NY.GDP.PCAP.CD)[1],
na.omit(vn_data_ok$NY.GDP.PCAP.CD)[length(na.omit(vn_data_ok$NY.GDP.PCAP.CD))],
# min(vn_data_ok$NY.GDP.PCAP.CD, na.rm = TRUE),
1000,
2000,
3000,
4000,
max(vn_data_ok$NY.GDP.PCAP.CD, na.rm = TRUE),
limit_truc_y_first[2] * scale_truc_y_second), digits = 0)
)
) +
labs(x = "Năm", y = "Dân số (triệu người)") +
labs(title = "Dân số và GDP bình quân đầu người ở Việt Nam 1960–2022",
subtitle = "Nguồn: WORLD BANK",
caption = "Nguồn: Dinh Le") +
scale_color_manual(values = c("#d7301f", "#238b45"),
# name = c("Chú thích"),
labels = c("Dân số (triệu người)", "GDP bình quân (USD/người)"),
# guide = guide_legend(title.position = "top")
) +
scale_linetype_manual(values = c("solid", "solid"),
# name = c("Chú thích"),
labels = c("Dân số (triệu người)", "GDP bình quân (USD/người)"),
# guide = guide_legend(title.position = "top")
) +
guides(color = guide_legend(title = NULL,
title.position = "top",
byrow = TRUE,
nrow = 1)) +
guides(linetype = guide_legend(title = NULL,
title.position = "top",
byrow = TRUE,
nrow = 1)) +
theme(axis.title.y.left = element_text(color = "#d7301f"),
axis.title.y.right = element_text(color = "#238b45", angle = 90)) +
theme(legend.key = element_rect(fill = "transparent", colour = "transparent"),
legend.key.size = unit(1.1, 'cm'),
# legend.spacing.x = unit(0.25, 'cm'),
# legend.spacing.y = unit(-0.25, 'cm'),
# legend.position = 'top',
# legend.justification = 'center',
legend.direction = 'horizontal',
legend.justification = c(0.5, 1),
legend.position = c(0.5, 1),
legend.background = element_rect(fill = "transparent",
linewidth = 1,
linetype = "solid",
colour = "transparent")) +
theme(plot.margin = margin(t = 1, r = 1, b = 1, l = 1, "cm")) +
theme(panel.background = element_rect(fill = "white"),
panel.border = element_rect(colour = "darkgreen", fill = NA, linewidth = 0.75),
plot.background = element_rect(fill = "#7fcdbb", colour = "black", linewidth = 0.75)) +
coord_cartesian(clip = 'off') +
theme(
# plot.title = element_text(size = 12, face = "bold", hjust = 0, family = "Times New Roman"),
# plot.subtitle = element_text(size = 11, hjust = 0, family = "Times New Roman"),
plot.caption = element_text(size = 10, hjust = 0),
plot.title.position = "plot",
# margin top, right, bottom, left
plot.title = ggtext::element_textbox_simple(size = 12, face = "bold",
padding = margin(5, 5, 5, 100)),
plot.subtitle = ggtext::element_textbox_simple(size = 11, face = "plain",
padding = margin(5, 5, 10, 100))
) +
theme(axis.text.x.bottom = element_text(face = "bold", color = "black", size = 10),
axis.text.y.left = element_text(face = "bold", color = "#d7301f", size = 10),
axis.text.y.right = element_text(face = "bold", color = "#238b45", size = 10)) +
theme(axis.title.x.bottom = element_text(face = "bold", color = "black", size = 11,
margin = margin(t = 10, r = 1, b = 1, l = 1)),
axis.title.y.left = element_text(face = "bold", size = 11,
margin = margin(t = 1, r = 5, b = 1, l = 1)),
axis.title.y.right = element_text(face = "bold", size = 11)) +
theme(axis.ticks.y.left = element_line(linewidth = 0.75, color = "#d7301f"),
axis.ticks.y.right = element_line(linewidth = 0.75, color = "#238b45"),
axis.ticks.x.bottom = element_line(linewidth = 0.75, color = "black"),
axis.ticks.length = unit(0.25, "cm")) +
geom_point(aes(x = 1993, y = 33), colour = "darkgreen") +annotate(geom = "curve", x = 1993, y = 33, xend = 1990, yend = 5,
curvature = -0.3, arrow = arrow(length = unit(0.04, "npc")), color = "darkgreen") +
annotate(geom = "text", x = 1988, y = 40,
label = "Mở cửa nền kinh tế", hjust = "left",
color = "darkgreen", fontface = "bold", size = 3) +
annotate("rect", xmin = 1988, xmax = 1992, ymin = 1, ymax = 10,
alpha = 0.2, color = "#238b45", fill = "#d7301f")
## Warning: Removed 25 rows containing missing values (`geom_line()`).
library(grid)
library(png)
vn_image <- readPNG("C:/Users/ufm_t/OneDrive/Desktop/tuhocR2/GDP_DS/Vn.PNG")
grid.raster(vn_image, x = 0.16, y = 0.89, width = 0.08)

# logor <- readPNG("C:/Users/ufm_t/OneDrive/Desktop/mar.PNG")
# grid.raster(logor, x = 0.91, y = 0.09, width = 0.08)