Interactive and static plots of military expenditure from 1949 to 2020.
1- Packages, I will use to read in and plot the data.
2- Read the data in from part 1.
Start with the data
Group_by region so there will be a ‘river’ for each region.
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
Use e_river to build ‘rivers’ that contain military spending by region. The depth of each river represents the amounts of dollar spent in military for each region
Use e_tooltip to add a tooltip that will display based on the axis values.
Use e_theme to change the theme to roma
regional_military_spending %>%
group_by(Region) %>%
mutate(Year = paste(Year, "12", "31", sep = "-")) %>%
e_charts(x= Year) %>%
e_river( serie = military_spending, legend = FALSE) %>%
e_tooltip(trigger = "axis") %>%
e_title(text = "Annual military spending, by world region",
subtext = "(in billions of dollars). Source: Our World in Data",
sublink = "https://ourworldindata.org/grapher/military-expenditure-total?tab=chart",
left= " center") %>%
e_theme("roma")
Use ggplot to create a new ggplot object. Use aes to indicate that Year will be mapped to the x axis; military spending will be mapped to the y axis; Region will be the fill variable.
geom_area will be display military spending.
scale_fill_ discrete_divergingx is a function in the colorspace package. It sets the color palette to roma and selects a maximum of 8 colors for the different regions.
theme_classic sets the theme.
theme(legend_position = “bottom”) puts the legend of the bottom of the plot
labs sets the y axis label, fill= NULL indicates that the fill variable will not have the labelled Region
regional_military_spending %>%
ggplot(aes(x = Year, y = military_spending,
fill = Region)) +
geom_area() +
colorspace:: scale_fill_discrete_divergingx(palette = "roma", nmax = 8) +
theme_classic() +
theme(legend.position = "bottom") +
labs(y = "in billions of dollars", fill= NULL)
These plots show a non steady movement in military spending since 1949. It may be explained by the correlation between world geopolitical or peace environment and military spending.