Brazil FDI Data

#Set up packages
library(readxl)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(writexl)
library(here) 
here() starts at /Users/galenerickson/Desktop/SAIS/Spring 2024/Brazil/Analysis/Brazil FDI Data
library(tidyr)
library(ggplot2)

The following data is found at: United Nations, “Foreign Direct Investment in Latin America and the Caribbean 2023”, available at: https://www.cepal.org/en/publications/48979-foreign-direct-investment-latin-america-and-caribbean-2023

#Import and analyze UN FDI data data
un_latam_fdi_2023 <- here("Input", "graficos_tablas_cap1_mu.xlsx")

# Specify the sheet and range and read the Excel file into a dataframe
un_latam_fdi_2023_data <- read_excel(un_latam_fdi_2023, range = "Cuadro I.2!B9:H45")
New names:
• `` -> `...1`
# Rename the first column to "country"
colnames(un_latam_fdi_2023_data)[1] <- "country"

# Filter rows where country is either "Brazil" or "Total"
filtered_data <- un_latam_fdi_2023_data %>%
  filter(country %in% c("Brazil", "Total"))

# Convert all columns except 'country' to character type
filtered_data <- filtered_data %>%
  mutate(across(-country, as.character))

# Reshape data to long format
long_data <- pivot_longer(filtered_data, 
                          cols = -country, 
                          names_to = "Year", 
                          values_to = "Value")

# Convert 'Value' from character to numeric
long_data$Value <- as.numeric(long_data$Value)

# Check if there were any conversion issues
sum(is.na(long_data$Value))
[1] 0
# Convert 'Value' from millions to billions
long_data$Value <- long_data$Value / 1000

#plot the graph
ggplot(long_data, aes(x = Year, y = Value, group = country, color = country)) +
  geom_line() +
  geom_point() + # Adds points to the line for better visualization
  theme_minimal() +
  scale_x_discrete(name = "Year") + # Adjusted for categorical x-axis
  scale_y_continuous(name = "Value (Billions)",
                     limits = c(0, NA), # Keep the lower limit at 0, automatically adjust upper limit
                     expand = expansion(add = c(0, 0))) + # Ensures Y-axis starts at 0
  labs(title = "Comparison of FDI Inflows Between Brazil and Total LATAM + Caribbean",
       x = "Year",
       y = "Value (Billions)",
       color = "Country") +
  scale_color_manual(values = c("Brazil" = "blue", "Total" = "red"))