Motivation

Recreate charts in: REPORT ON SOCIO-ECONOMIC SITUATION IN THE 4TH QUARTER AND 2023

Source: GSO

Economic Growth: In the overall economic growth, the agriculture, forestry, and fisheries sector increased by 3.83%, contributing 8.84%; the industry and construction sector increased by 3.74%, contributing 28.87%; and the service sector increased by 6.82%, contributing 62.29%.

Data Processing

#Setwd
setwd("D:/0 - My documents/TOOLS/R/GSO")

#Step 1: Load data 
library(rio)
gdp <- import("D:/0 - My documents/TOOLS/R/GSO/Data.xlsx", sheet = "gdp")

  # Rename
  # library(janitor)
  # gdp <- gdp %>%
    # standardize column name syntax
    # janitor::clean_names()

names(gdp)

# Change structure wide to long
library(tidyverse)
gdp_long <- gdp %>% 
  pivot_longer(
    cols = "GDP":"Services",
    names_to = "section",
    values_to = "number"
  )

Visualization

# Visualization

gdp_long$section <- factor(gdp_long$section, levels = c("GDP",
                                                        "Agriculture, Forestry, and Fisheries",
                                                        "Industry and construction",
                                                        "Services"))

text_gdp <- gdp_long %>% 
  filter(section == "GDP" & Quarter != "Q4")

text_gdp1 <- gdp_long %>% 
  filter(section == "GDP" & Quarter == "Q4")

text_agri <- gdp_long %>% 
  filter(section == "Agriculture, Forestry, and Fisheries")

text_industry <- gdp_long %>% 
  filter(section == "Industry and construction"& Quarter != "Q4")

text_industry1 <- gdp_long %>% 
  filter(section == "Industry and construction"& Quarter == "Q4")

text_ser <- gdp_long %>% 
  filter(section == "Services")

text <- "In the overall economic growth, <span style = 'color:#9BCF53'>**the agriculture, forestry, and fisheries sector**</span> increased by 3.83%, contributing 8.84%; <span style = 'color:#6420AA'>**the industry and construction sector**</span> increased by 3.74%, contributing 28.87%; and <span style = 'color:#FAA300'>**the service sector**</span> increased by 6.82%, contributing 62.29%."
df_text <- data.frame(Quarter = "Q3", section = "GDP", number = 1, text = text)

library(ggtext)
  
ggplot(gdp_long, aes(Quarter, number, group = section, color = section))+
  geom_rect(aes(xmin = "Q3", xmax = "Q4", ymin = -Inf, ymax = 9), fill = "#E3E1D9", colour = "#E3E1D9") +
  geom_line(aes(linetype = section, linewidth = section), show.legend = TRUE)+
  geom_point(size = 2)+
  theme_minimal()+
  # Edit background
  theme(axis.title = element_blank())+
  theme(axis.text.y = element_blank())+
  theme(panel.grid.minor = element_blank())+
  theme(panel.grid.major.x = element_blank())+
  theme(panel.grid.major.y = element_line(color = "#E3E1D9", linewidth = 0.1))+
  theme(plot.background = element_rect(fill = "#F6F4EB", color = "#F6F4EB")) +
  theme(panel.background = element_rect(fill = "#F6F4EB", color = "#F6F4EB")) +
  theme(legend.position = "bottom")+
  # Edit line color, width, type
  scale_color_manual(values=c("#40A2E3","#9BCF53","#6420AA","#FAA300"))+
  scale_linewidth_manual(values = c(2, 1, 1, 1)) +
  scale_linetype_manual(values = c("dashed", "solid", "solid", "solid"))+
  # Scale x, y
  scale_y_continuous(breaks = c(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
                     limits = c(-1,9)) +
  scale_x_discrete(expand = c(0.05, 0))+
  # Add title, sub, caption
  labs(title = "Figure 1: Quarterly GDP Growth Rates Across Sectors in 2023 (%)",
       subtitle = "Sources: GSO",
       caption = "Recreate by: Thao Bui")+
  # Axis ticks
  theme(axis.ticks.x = element_line(color = "grey30", linewidth = 0.2))+
  # Edit 
  theme(plot.title = element_text(size = 12,face = "bold", color = "grey30")) +
  theme(plot.subtitle = element_text(size = 10, face = "italic", color = "grey50")) +
  theme(plot.caption = element_text(size = 7, face = "italic", color = "grey50")) +
  theme(axis.text.x = element_text(size = 7, color = "grey30")) +
  theme(legend.title = element_blank())+
  theme(legend.text = element_text(size = 7, color = "grey30"))+
  # Adjust plot margin
  theme(plot.margin = unit(c(0.5, 0.5, 0.1, 0.5), "cm"))+
  # Add text
  geom_text(data = text_gdp, aes(Quarter, number+0.5, label = number), size = 3,
            color = "#40A2E3", show.legend = FALSE)+
  geom_text(data = text_gdp1, aes(Quarter, number-1, label = number), size = 3,
            color = "#40A2E3", show.legend = FALSE)+
  geom_text(data = text_agri, aes(Quarter, number-0.5, label = number), size = 3,
            color = "#9BCF53", show.legend = FALSE)+
  geom_text(data = text_industry, aes(Quarter, number-0.5, label = number), size = 3,
            color = "#6420AA", show.legend = FALSE)+
  geom_text(data = text_industry1, aes(Quarter, number-1, label = number), size = 3,
            color = "#6420AA", show.legend = FALSE)+
  geom_text(data = text_ser, aes(Quarter, number+0.5, label = number), size = 3,
            color = "#FAA300", show.legend = FALSE)+
  # Add text box
  geom_textbox(
    data = df_text,
    aes(x = Quarter, y = 3, label = text, box.color = NA),
    width = 0.31, 
    hjust = 0, 
    vjust = 1,
    fill = NA, 
    color = "grey30",
    size = 2.2
  )

  ggsave("gdp.png", width = 6, height = 4,dpi = 300,units = c("in"))