Nations Dataset Plots

GDP written in colorful wooden letters

Source: Jernej Furman, Creative Commons License
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(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
options(dplyr.summarise.inform = FALSE) #Get rid of message after group_by

nations <- read_csv("nations.csv", show_col_types = FALSE)

#Mutate a new GDP column
nations_gdp <- mutate(nations, gdp = ((gdp_percap * population)/10^12))

Dot-and-Line Chart: GDP and the WPS

The following four countries were selected from Georgetown University’s Women Peace and Security Index ranking. Denmark is the highest ranked country, with Switzerland in second place. Burundi occupies the 172nd spot (out of 177) and the Central African Republic the 175th. Afghanistan and the Republic of Yemen technically positioned as worst and second-worst, respectively, but both were missing values that would have resulted in a misleading visualization.

nations_dot_chart <- filter(nations_gdp, country == "Denmark" |
                                         country == "Switzerland" | 
                                         country == "Burundi" |
                                         country == "Central African Republic") 

#To ensure the legend is in the correct order
nations_dot_chart$country <- factor(nations_dot_chart$country, levels = c("Denmark", "Switzerland", "Burundi", "Central African Republic"))

nations_dot_chart |>
  ggplot(aes(x = year, y = gdp, color = country)) +
  labs(title = "Gross Domestic Misogyny:",
       subtitle = "GDP Growth in Best and Worst Countries for Women",
       caption = "Source: class dataset") +
  xlab("Year") +
  ylab("GDP ($ trillion)") +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1", name = "") #legend title seems unnecessary

Make it interactive

nations_dot_int <- filter(nations_gdp, country == "Denmark" |
                                       country == "Switzerland" | 
                                       country == "Burundi" |
                                       country == "Central African Republic") |>
ggplot(aes(x = year, y = gdp, color = country)) +
  labs(title = "Gross Domestic Misogyny:GDP Growth in Best and Worst Countries for Women") +
  xlab("Year") +
  ylab("GDP ($ trillion)") +
  geom_line() +
  geom_point() +
  scale_color_brewer(palette = "Set1", name = "") 
nations_dot_int <- ggplotly(nations_dot_int) |>
  hide_legend()
nations_dot_int

GDP by World Bank Region

gdp_by_region <- nations_gdp |> 
  group_by(region, year) |>
  summarise(GDP = sum(gdp, na.rm = TRUE)) |>
  ggplot(aes(x = year, y = GDP, fill = region)) +
  geom_line() +
  geom_area(color = "white") +
  scale_fill_brewer(palette = "Set2", name = "Region") +
  labs(title = "GDP by World Bank Region",
       caption = "Source: class dataset") +
  xlab("Year") +
  ylab("GDP ($ trillion)") 

gdp_by_region

Make it interactive

gdp_by_region_int <- ggplotly(gdp_by_region) |>
  hide_legend()
gdp_by_region_int