Project Part 2

Interactive and Static plots for armed forces per country from 1990 to 2019

  1. Packages I will use to read in and plot the data
  1. Read the data in from part 1
armed_forces_per_country  <- read_csv(here::here("armed_forces_per_country.csv"))

Interactive graph

-Start with the data

-Group_by region so there will be a “river” for each country

-Use mutate to round armed_forces_per_country so only 2 digits will be displayed when you hover over it.

-Use mutate to change Year so it will be displayed as end of year instead of beginning of year

-Use e_charts to create an e_charts object with Year on the x axis

-e_river to build “rivers” that contain armed_forces_per_country. The depth of each river represents the number of forces for each country.

-e_tooltip to add a tooltip that will display based on the axis values

-Use e_title to add a title, subtitle, and link to subtitle

-Use e_theme to change the theme to roma

armed_forces_per_country   %>%
  group_by(Country)  %>%
  mutate(Armed_Forces = round(Armed_Forces, 4),
         Year = paste(Year, "12", "31", sep="-"))  %>% 
  e_charts(x = Year)   %>% 
  e_river(serie = Armed_Forces, legend=FALSE)  %>% 
  e_tooltip(trigger = "axis")  %>% 
  e_title(text = "Armed forces count per country",
          subtext = "(in billions). Source: Our World in Data",
          sublink = "https://ourworldindata.org/grapher/armed-forces-personnel?tab=chart",
          left = "center")  %>% 
  e_theme("roma")  

Static graph

-Start with the data

-Use ggplot to create a new ggplot object. Use aes to indicate that Year will be mapped to the x axis; Armed_Forces will be mapped to the y axis; Country will be the fill variable

-geom_area will display Armed_Forces

-scale_fill_discrete_divergingx is a function in the colorspace package. It sets the color palette to roma and selects a maximum of 12 colors for the different regions

-theme_classic sets the theme

-theme(legend.position = “bottom”) puts the legend at the bottom of the plot

-labs sets the y axis label, fill = NULL indicates that the fill variable will not have the labelled Region

armed_forces_per_country   %>% 
  ggplot(aes(x = Year, y = Armed_Forces, 
             fill = Country)) +
  geom_area() +
  colorspace::scale_fill_discrete_divergingx(palette = "roma", nmax =11) +
  theme_classic() +
  theme(legend.position = "bottom") +
  labs( y = "in billions of tonnes",
       fill = NULL)

The plots illustrate how the number of armed forces per country has increased over time since 1990.

ggsave(filename = here::here("_posts/2022-05-02-project-part-2/preview.png"))