Military: 軍事費の比較, r ver. 1

1.何をする?

 

世界の軍事について学びます。

手はじめにWikipediaのデータをグラフにします。

 

https://en.wikipedia.org/wiki/List_of_countries_by_military_expenditures

 

2.軍事費の国際比較

 

WikipediaのHTMLテーブルからデータを取得しグラフにします。

 

スクリプト

 

install.packages("htmltab")
library(htmltab)
library(tidyverse)
url <- "https://en.wikipedia.org/wiki/List_of_countries_by_military_expenditures"
df0 <- htmltab(doc = url, which = 2) # 2番目の表
df0
df0_subset <- df0[c(2:4)] # 列を指定
df0_subset_tibble <- as_tibble(df0_subset) # 扱いやすいようにtibbleに変換
df0_subset_tibble
# 桁の,を削除して、文字列を数に変換して、桁の,を除いて、World total行を削除する。
df0_subset_tibble %>% 
  mutate(spending_us_billion = as.numeric(gsub(",", "", df0_subset_tibble$`Spending  (US$ bn)`)),
         gdp_percent = as.numeric(gsub(",", "", df0_subset_tibble$`% of GDP`))) %>%
  filter(Country != c("World total")) -> df1
# military expenditure USA$のグラフを描く
p1 <- ggplot(df1, mapping = aes(x=reorder(Country, gdp_percent), gdp_percent,
                                          fill = Country))
p1 + geom_bar(stat="identity") +
  guides(fill=FALSE) +
  xlab("Country") +
  ylab("Expenditure/GDP(%)") +
  ggtitle("Military Expenditure(US$) SIPRI 2019") +
  coord_flip()
# military expenditure/GDPのグラフを描く
p1 <- ggplot(df1, mapping = aes(x=reorder(Country, spending_us_billion), spending_us_billion,
                                fill = Country))
p1 + geom_bar(stat="identity") +
  guides(fill=FALSE) +
  xlab("Country") +
  ylab("Spending (US$)") +
  ggtitle("Military Expenditure/GDP(%) SIPRI 2019") +
  coord_flip()

 

軍事費(USAドル)の比較

 

GDPに対する軍事費の割合の比較

 

 

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.