options(repos = c(CRAN = "https://cran.rstudio.com/"))
library(tidyverse)
install.packages('WDI')
## package 'WDI' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\cngil\AppData\Local\Temp\Rtmp6DlW9b\downloaded_packages
library(WDI)
library(plotly)
library(scales)
indicators <- c("SP.POP.TOTL", # Population
"SG.GEN.PARL.ZS", # Proportion of seats held by women in national parliaments (%)
"NY.GDP.PCAP.KD") # GDP per capita
wdi_parl_raw <- WDI(country = "all", indicators, extra = TRUE,
start = 2000, end = 2019)
wdi_clean <- wdi_parl_raw %>%
filter(region != "Aggregates") %>%
select(iso2c, iso3c, country, year,
population = SP.POP.TOTL,
prop_women_parl = SG.GEN.PARL.ZS,
gdp_per_cap = NY.GDP.PCAP.KD,
region, income)
Do the following:
geom_point()).wdi_2019 <- wdi_clean %>%
filter(year == 2019) %>%
drop_na(prop_women_parl) %>%
mutate(prop_women_parl = prop_women_parl / 100,
region = fct_rev(region))
static_plot <- ggplot(wdi_2019,
aes(y = region, x = prop_women_parl, color = region)) +
geom_point(position = position_jitter(width = 0, height = 0.15, seed = 1234)) +
guides(color = "none") +
scale_x_continuous(labels = percent) +
scale_color_manual(values = c("#425300", "#e680ff", "#01bd71", "#ff3aad",
"#9f3e00", "#0146bf", "#671d56")) +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw()
static_plot
ggplotly().ggplotly(static_plot)
static_plot_toolip <- ggplot(wdi_2019,
aes(y = fct_rev(region), x = prop_women_parl, color = region)) +
geom_point(aes(text = country),
position = position_jitter(width = 0, height = 0.15, seed = 1234)) +
guides(color = "none") +
scale_x_continuous(labels = percent) +
scale_color_manual(values = c("#425300", "#e680ff", "#01bd71", "#ff3aad",
"#9f3e00", "#0146bf", "#671d56")) +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw()
## Warning in geom_point(aes(text = country), position = position_jitter(width =
## 0, : Ignoring unknown aesthetics: text
static_plot_toolip
ggplotly(static_plot_toolip, tooltip = "text")
wdi_2019 <- wdi_clean %>%
filter(year == 2019) %>%
drop_na(prop_women_parl) %>%
# Scale this down from 0-100 to 0-1 so that scales::percent() can format it as
# an actual percent
mutate(prop_women_parl = prop_women_parl / 100) %>%
mutate(fancy_label = paste0(country, "<br>",
percent(prop_women_parl, accuracy = 0.1),
" women in parliament"))
static_plot_toolip_fancy <- ggplot(wdi_2019,
aes(y = fct_rev(region),
x = prop_women_parl,
color = region)) +
geom_point(aes(text = fancy_label),
position = position_jitter(width = 0, height = 0.15, seed = 1234)) +
guides(color = "none") +
scale_x_continuous(labels = percent) +
# I used https://medialab.github.io/iwanthue/ to generate these colors
scale_color_manual(values = c("#425300", "#e680ff", "#01bd71", "#ff3aad",
"#9f3e00", "#0146bf", "#671d56")) +
labs(x = "% women in parliament", y = NULL, caption = "Source: The World Bank") +
theme_bw()
## Warning in geom_point(aes(text = fancy_label), position = position_jitter(width
## = 0, : Ignoring unknown aesthetics: text
ggplotly(static_plot_toolip_fancy, tooltip = "text")
interactive_plot <- ggplotly(static_plot_toolip_fancy, tooltip = "text")
htmlwidgets::saveWidget(interactive_plot, "fancy_plot.html")
Good luck and have fun!