Elections: 2016年米国大統領選の選挙結果 r, geofacet ver. 1

Last Updated on November 16, 2020 by shibatau

I.何をする

 

2016年大統領選の選挙結果をグラフにします。Rのgeofacetには、サンプル・データとして選挙結果が含まれており、簡単い州別のデータを地図にマップできます。

次に解説されています。

 

Introduction to geofacet

 

II.選挙結果データ

 

2016年の大統領選の州別投票結果のデータはelectionの名で保存されています。

 

> head(election)
    state candidate   votes       pct
1 Alabama   Clinton  729547 34.357946
2 Alabama     Trump 1318255 62.083092
3 Alabama     Other   75570  3.558962
4  Alaska   Clinton  116454 36.550871
5  Alaska     Trump  163387 51.281512
6  Alaska     Other   38767 12.167617

 

III.全スクリプト

 

library(tidyverse)
library(geofacet)
# show the data
head(election)
# cumulative bar chart
ggplot(election, aes("", pct, fill = candidate)) +
  geom_col(alpha = 0.8, width = 1) +
  scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
  facet_geo(~ state, grid = "us_state_grid2") +
  scale_y_continuous(expand = c(0, 0)) +
  labs(title = "2016 Election Results, cumulative bar chart",
       caption = "Data Source: http://bit.ly/2016votecount",
       x = NULL,
       y = "Percentage of Voters") +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text.x = element_text(size = 6))
# bar chart
ggplot(election, aes(candidate, votes / 1000000, fill = candidate)) +
  geom_col() +
  scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
  facet_geo(~ state, grid = "us_state_grid2") +
  coord_flip() +
  labs(title = "2016 Election Results, bar chart",
       caption = "Data Source: http://bit.ly/2016votecount",
       x = NULL,
       y = "Votes (millions)") +
  theme(strip.text.x = element_text(size = 6))
# percentagae component bar chart
ggplot(election, aes(candidate, votes / 1000000, fill = candidate)) +
  geom_col() +
  scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
  facet_geo(~ state, grid = "us_state_grid2") +
  coord_flip() +
  labs(title = "2016 Election Results, percentage of voters",
       caption = "Data Source: http://bit.ly/2016votecount",
       x = NULL,
       y = "Votes (millions)") +
  theme(strip.text.x = element_text(size = 6))

 

スクリプトをKaggleのノートでも公開しました。

 

https://www.kaggle.com/shibatau/2016election

 

IV.グラフ

 

積算棒グラフ

 

 

棒グラフ

 

 

割合の棒グラフ

 

合計で1になるはずです。一部うまく集計されていないようですが、、、。

 

 

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.