rm(list=ls(all=TRUE))
librarian::shelf(
tidyverse
, WDI
, forcats
, writexl
)
user <- Sys.getenv("USERNAME")
setwd( paste0("C:/Users/",user,"/Documents/GitHub/1ECO35_2022_2/Lab10") )
help(WDI)
data <- WDI(indicator = "NY.GDP.PCAP.KD", start = 2018, end = 2018)
country_code <- as_tibble(WDI_data$country)
data_plot <- data |>
rename(GDP = NY.GDP.PCAP.KD) |>
inner_join(country_code, by = "iso2c") |>
filter(region != "Aggregates") |>
top_n(-30, GDP) |>
mutate(country = fct_reorder(country.x, GDP))
attributes(data_plot)
data_plot |> ggplot() +
aes(x = country, y = GDP, fill = region) +
geom_bar(stat = "identity", alpha = .6, width = .5) +
geom_text(
aes(label = format(round(GDP, 1), nsmall = 1)),
hjust = - 0.35,
size = 3
) +
coord_flip(ylim = c(0, 2000)) +
geom_hline(yintercept = 0, alpha = 0.5) +
xlab("") +
ylab("GDP per capita (constant 2000 US$) in 2020") +
theme_classic() +
scale_fill_brewer(palette = "Set2", name = "Region") +
theme(
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
legend.text = element_text(size = 8),
axis.title.x = element_text(size = 10)
)
librarian::shelf(
ggrepel
, scales
)
data <- WDI(indicator = "NY.GDP.PCAP.KD", start = 1980, end = 2020)
data_plot <- data %>%
rename(GDP = NY.GDP.PCAP.KD) %>%
inner_join(country_code, by = "country") %>%
filter(year %in% c(1980, 2020), region != "NA") %>%
group_by(iso3c.x) %>%
pivot_wider(names_from = "year", values_from = "GDP") %>%
ungroup() |>
rename (pc_gdp_1980 = `1980`, pc_gdp_2020 = `2020`) |>
mutate(pc_gdp_2020 = pc_gdp_2020/1000, pc_gdp_1980 = pc_gdp_1980/1000)
country_stress <- c("USA","CHN","BRA")
ggplot(data_plot , aes(x = pc_gdp_1980, y = pc_gdp_2020)) +
geom_point(aes(alpha = 0.8, color = (iso3c.x %in% country_stress)), show.legend = FALSE) +
geom_abline(slope = 1, color = "gray") +
geom_text_repel(aes(label = ifelse(iso3c.x %in% country_stress, iso3c.x, ""))) +
scale_y_continuous(
limits = c(0, 100),
breaks = c(0, 25,50, 75,100),
labels = expression(0, 25,50,75, 100)
) +
scale_x_continuous(
limits = c(0, 100),
breaks = c(0, 25,50,75, 100),
labels = expression(0, 25,50,75, 100)
scale_color_manual(values = c("gray", "red")) + ) +
xlab("GDP per capita (constante 2000 miles-US$) 1980") +
ylab("GDP per capita (cosntante R milesUS$) 2020") +
theme_classic() +
theme(
axis.text = element_text(size = 12),
axis.title = element_text(size = 10)
)
ggsave("../plots/scatter.png"
, height = 8
, width = 12
, dpi = 320
)