Skip to content

Commit

Permalink
include vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed Sep 26, 2024
1 parent f31da6b commit c464220
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion vignettes/milRex.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,90 @@ knitr::opts_chunk$set(
)
```

```{r setup}
# Getting Data

Querying the Military Expenditure Database by SIPRI can be done through their [website](https://milex.sipri.org/sipri), however, the resulting xlsx is rather tedious to parse because of some formatting choices taken by the team.

Hence this function makes it easy to directly import this database to R and parse not only the different indicators, but also the associated footnotes.

The following code is all you need to query data from their website.
```{r querying, message=F, warning=F}
library(milRex)
milex <- sipri_get_data(indicator = "constantUSD")
```

The available indicators are:

```{r labels, echo = F, message=F, warning=F}
library(dplyr)
library(knitr)
dplyr::tribble(
~sheet_name, ~unit,~skip,~indicator,
"Constant (2022) US$", 2, 5,'constantUSD',
"Current US$", 2, 5,'currentUSD',
"Share of GDP", 1, 5,'shareOfGDP',
"Share of Govt. spending", 3, 7,'shareGovt',
"Regional totals", 2, 13,'regionalTotals',
"Local currency financial years", 2, 7,'currencyFY',
"Local currency calendar years", 2, 6,'currencyCY',
"Per capita", 2, 6,'perCapita',
"all of the above", 2, 6,'all'
) |>
dplyr::select(sheet_name, indicator) |>
knitr::kable()
```

# Aggregating data by regions

The package also comes with a pre-packaged data-frame that allows you to merge
SIPRIs regional coding to the data-frame.


```{r, message=F, warning=F}
library(tidyverse)
milex_regional <- milex |>
left_join(milRex::sipri_regions) |>
summarise(value = sum(value, na.rm = T), .by = c(region, year)) |>
filter(year >= 1988)|>
mutate(value = if_else(year == 1991, 0, value)) |>
mutate(year = paste(year, "01-01") |> ymd())
```

Now we can replicate a common graph used by SIPRI to show military spending by regions.

```{r,warning=F, message=F,out.width="100%",dpi=300}
library(ggplot2)
ggplot(milex_regional) +
geom_col(aes(year, value/1000, fill = reorder(region, value)))+
labs(y = "Militarxy expenditure\n (constant 2022 USD billion)",
caption = "1991 not present because no values for Soviet Union.\n See https://www.sipri.org/sites/default/files/2024-04/2404_fs_milex_2023.pdf",
fill = "Region",
x = "") +
theme_bw() +
theme(plot.background = element_rect(fill = rgb(215, 229, 221,
maxColorValue = 255)),
panel.background = element_rect(fill = rgb(215, 229, 221,
maxColorValue = 255)),
legend.background = element_rect(fill = rgb(215, 229, 221,
maxColorValue = 255)),
legend.position = "top",
legend.text = element_text(size = 4),
legend.title = element_text(size = 4,face = "bold"),
panel.border = element_blank(),
axis.line.x = element_line(),
text = element_text(size = 7),
legend.key.size = unit(0.05, 'in'))+
scale_fill_manual(values =
c(
rgb(83,50,82, maxColorValue = 255),
rgb(118,81,116, maxColorValue = 255),
rgb(186,144,177, maxColorValue = 255),
rgb(154,173,164, maxColorValue = 255),
rgb(100,156,136, maxColorValue = 255)
))+
scale_x_date(breaks = "4 years",date_labels = "%Y")
```


0 comments on commit c464220

Please sign in to comment.