# Finding data from the World Bank Indicators
library(wbstats)
# Just in case, but probably not necessary
<- wb_cache()
WBCache # SI.POV.GINI, 6.0.GDPpc_constant will be downloaded in one dataset
<- wb_data(country = "countries_only", indicator = c("SI.POV.GINI", "NY.GDP.PCAP.KD"))
WBData # Cleaning data removing missing values.
<- WBData[-c(which(is.na(WBData$SI.POV.GINI) | is.na(WBData$`NY.GDP.PCAP.KD`))),]
clean_data # Add country regions for more effective groupings
<- wb_countries()
wb_countries <- merge(clean_data, y = wb_countries[c("iso2c", "region")], by = "iso2c", all.x = TRUE)
wb_dat # Make neater column names
colnames(wb_dat)[5] <- "GDPpC"
colnames(wb_dat)[6] <- "Gini"
# Create an interaction to show more info when grouping in ggplot?
$CountryDate <- interaction(wb_dat$country, wb_dat$date) wb_dat
Dynamic Graphs
Dynamic Graphs
Now to begin plotting:
library(ggplot2)
library(viridis)
library(plotly)
<- highlight_key(wb_dat, ~country)
gini_highlight <- ggplot(data = gini_highlight,
gini_p_hl mapping = aes(x = GDPpC,
y = Gini,
color = region))
<- gini_p_hl + geom_point(alpha = 0.6, aes(group = CountryDate)) + geom_smooth(method = "loess", alpha = 0.3)
gini_p_hl <- gini_p_hl + scale_x_continuous(trans = "log10", labels = scales::dollar_format(), limits = c(200,120000)) +
gini_p_hl ylab('Inequality(gini)') + xlab('GDP per Capita') + labs(title = 'Kuznets Curves',
caption = 'Narrow the region using the legend, hover over a point to see the country and date of the datum. Click on the point to highlight other points for the same country') +
scale_color_viridis(discrete = TRUE, option = "H") +
theme_classic(base_size=12)
<- ggplotly(gini_p_hl, tooltip = c("CountryDate","GDPpC")) %>%
gini_p_hl_plotly highlight(on = "plotly_click",
off = "plotly_relayout",
opacityDim = 0.2)
gini_p_hl_plotly
Narrow the region using the legend, hover over a point to see the country and date of the datum. Click on the point to highlight other points for the same country. By isolating the region it helps to see the general trend the kuznets curve follows.