class: center, middle, inverse, title-slide # Visualizing Election Results with ggplot2 ## Taiwan 2020 Election & NTU Student Congress ### Dennis Tseng ### Taiwan R User Group ### 2020/01/20 --- <style type="text/css"> .remark-slide-content { font-size: 28px; padding: 1em 2em 1em 2em; } .my-one-page-font { font-size: 10px; } </style> # Self Introduction <ul> <li>Background <ul> <li>NTU Business Administration</li> <li>CRM Analytics Intern</li> </ul> <li>Interest <ul> <li>Data Analyst Track</li> <li>Data Journalist Track</li> </ul> <li>R <ul> <li>package: dplyr, tidyr, purrr, ggplot2, shiny</li> <li>data importing, cleaning, manipulation, visualization; social network; data mining, text mining</li> </ul> <li>More <ul> <li><a href="https://medium.com/dennis-r-data-news" target="_blank">My Medium</a></li> <li><a href="https://dennisrdatanews.netlify.com/">My Blog</a></li> </li> </ul> --- # Agenda <ul> <li>Map <ul> <li>背景地圖/Background Map</li> <li>統計地圖/Choropleth</li> <li>點示地圖/Dot Distribution Map; Bubble Map</li> <li>示意地圖/Cartogram</li> <li>國會席次圖/Parliament Plots</li> <li>六邊型網格圖/Hexmap/Tilegram</li> </ul> <li>Non-Map <ul> <li>散點圖/Scatterplot</li> <li>堆疊長條圖/Stacked Bar Plot</li> <li>折線子圖/Facet Line Plot</li> <li>美化圖表/Aesthetic specifications</li> </ul> --- # Nice Work: Map <img src="image/端_map_cartogram.png" width="45%" height="45%" /><img src="image/viewsoftheworld_cartogram.png" width="45%" height="45%" /> .pull-left[Taiwan 2020 Presidential Choropleth by [端傳媒](https://theinitium.com/article/20200112-taiwan-election-data-ntu/)] .pull-right[US 2016 Presidential Vote Share Map by [Views of the World](http://www.viewsoftheworld.net/wp-content/uploads/2016/11/USelection2016Cartogram.png)] --- # Nice Work: Map <img src="image/NYT_map_bubble.png" width="45%" height="45%" /><img src="image/端_map_hexbin.png" width="45%" height="45%" /> .pull-left[US 2012 Presidential Vote Lead Map by [The New York Times](https://www.nytimes.com/interactive/2016/11/01/upshot/many-ways-to-map-election-results.html)] .pull-right[Taiwan 2020 Parliament Hexmap by [端傳媒](https://theinitium.com/article/20200112-taiwan-election-data-ntu-1/)] --- # Nice Work: Map <img src="image/reddit_map_hex_swing.png" width="50%" height="50%" /><img src="image/關鍵_plot_parliament.png" width="50%" height="50%" /> .pull-left[UK 2019 Parliament Swing Map by [TeHuia](https://www.reddit.com/r/MapPorn/comments/eah5j1/uk_2019_election_swing_map/)] .pull-right[Taiwan 2020 Parliament Plot by [關鍵評論網](https://www.thenewslens.com/article/129934)] --- # Data <ul> <li>Taiwan 2020 Election Results <ul> <li>Presidential Election</li> <li>Parliament Election</li> </ul> <li>NTU Student Congress Election & Attedance <ul> <li>Vote</li> <li>Attendance</li> </ul> </ul> --- # Map: Background Map <img src="image/R_map_background.png" width="70%" height="70%" /> --- # Spatial Data in R <ul> <li>Simple Feature <ul> <li>sf, the table (data.frame) with feature attributes and feature geometries.</li> <li><a href="http://r-spatial.github.io/sf/articles/sf1.html" target="_blank">Simple Features for R</a></li> <li>library(sf)</li> </ul> <li>Raster <ul> <li>raster, a spatial format consists of multiple cells/pixels. Each cell represent an area, and has its own value. The value can mapping to spatial features such as temperature.</li> <li><a href="https://www.neonscience.org/dc-raster-data-r" target="_blank">Spatial Data in R: raster</a></li> <li>library(raster)</li> </ul> </ul> --- # Geographical Data in R: sf <img src="image/R_example_sf.png" width="95%" height="95%" /> --- # Geographical Data in R: raster <img src="image/R_example_raster.png" width="95%" height="95%" /> --- # Raw Spatial Data: GeoJSON <li>GeoJSON <ul> <li>A format based on JS Object Notation, designed for representing geographical features as well as their non-spatial attributes. The features include points, line strings, polygons, and multi-part collections of these types.</li> <li><a href="https://cran.r-project.org/web/packages/geojsonR/vignettes/the_geojsonR_package.html" target="_blank">Processing of GeoJSON data in R</a></li> <li>library(geojsonR)</li> </ul> <img src="image/R_example_geojson.png" width="55%" height="55%" /> --- # Geographical Data: Taiwan - shp <ul> <li>Process <ul> <li>Download raw data: shp</li> <li>Simplify shapefile with mapshaper</li> <li>Import shapefile with library(sf)</li> </ul> <li>Links <ul> <li><a href="https://data.gov.tw/dataset/7442" target="_blank">直轄市、縣市界線(TWD97經緯度)</a></li> <li><a href="https://mapshaper.org/" target="_blank">mapshaper</a></li> <li><a href="https://www.facebook.com/imDataMan/posts/1849991925279765" target="_blank"> Data Man 的資料視覺化筆記介紹 mapshaper</a></li> </ul> </ul> --- # Geographical Data: Taiwan - geojson <ul> <li>Process <ul> <li>Download raw data: geojson</li> <li>Import shapefile with library(geojsonio)</li> </ul> <li>Links <ul> <li><a href="https://github.com/codeforamerica/click_that_hood/blob/master/public/data/taiwan.geojson">taiwan.geojson</a></li> <li><a href="https://medium.com/@imandylin2_38094/%E5%A4%A9%E4%B8%8B%E9%9B%9C%E8%AA%8C2018%E5%8F%B0%E7%81%A3%E9%81%B8%E8%88%89%E5%9C%B0%E5%9C%96%E8%A3%BD%E4%BD%9C%E5%88%86%E4%BA%AB-%E6%8A%80%E8%A1%93%E7%9A%84%E9%83%A8%E5%88%86-de82c38da77b" target="_blank"> Building 2018 Taiwan Election Map by 林佳賢</a></li> </ul> </ul> --- class: inverse, center, middle # R time: Background Map --- # Map: Choropleth <ul> <li>Definition <ul> <li>A choropleth map displays divided geographical areas or regions that are coloured in relation to a numeric variable.</li> <li><a href="https://www.r-graph-gallery.com/choropleth-map.html">Choropleth Map by the R Graph Gallery</a></li> </ul> <li>Process <ul> <li>Get Taiwan 2020 Election Results Data</li> <li>Decide the variable plotted, such as KMT vote per</li> <li>Join Election with Taiwan sf object</li> <li>Draw the map colored by the variable</li> </ul> </ul> --- class: inverse, center, middle # R time: Choropleth --- # Map: Dot Distribution Map/Bubble Map <ul> <li>Definition <ul> <li>Choropleths aggregate individual data points into a single geographic region. In contrast, a dot distribution/density map uses a dot symbol to show the presence of a feature or a phenomenon.</li> <li><a href="https://www.r-graph-gallery.com/bubble-map.html">Bubble map by the R Graph Gallery</a></li> </ul> <li>Process <ul> <li>Get Taiwan Cities Population Data</li> <li>Decide the variable plotted, such as city population</li> <li>Plot the base map and add city dots</li> </ul> </ul> --- class: inverse, center, middle # R time: Dot Distribution Map --- # Map: Cartogram <ul> <li>Definition <ul> <li>A cartogram is a map in which the geometry of regions is distorted in order to convey the information of an alternate variable.</li> <li><a href="https://www.r-graph-gallery.com/cartogram.html">Cartogram by the R Graph Gallery</a></li> </ul> <li>Process <ul> <li>Get Taiwan 2020 Election Results Data and population data</li> <li>Decide the variable for distortion, such as population</li> <li>Also decide the variable cared about, such as KMT vote per</li> <li>Distort the sf object based on the variable</li> <li>Plot the distorted map and colored by chosen variable</li> </ul> </ul> --- class: inverse, center, middle # R time: Cartogram --- # Map: Parliament Plots <ul> <li>Definition <ul> <li>A visual representations of the composition of legislatures that display seats colour-coded by party.</li> <li><a href="https://github.com/RobWHickman/ggparliament">ggparliament by RobWHickman</a></li> <li><a href="https://erocoar.github.io/ggpol/">ggpol by Frederik Tiedemann</a></li> </ul> <li>Process <ul> <li>Get Taiwan 2020 Parliament Raw Data</li> <li>Create x, y, and theta columns</li> <li>Plot Parliament Composition</li> </ul> </ul> --- class: inverse, center, middle # R time: Parliament Plots --- # Map: Hexmap <ul> <li>Definition <ul> <li>A a geospatial object where all regions of the map are represented as hexagons.</li> <li><a href="https://pitchinteractiveinc.github.io/tilegrams/">tilegramps showcase with JS</a></li> <li><a href="https://github.com/olihawkins/clhex">library(clhex)</a></li> <li><a href="https://olihawkins.com/project/hexjson-editor/">Hexmap Editor</a></li> </ul> <li>Process <ul> <li>Create empty Taiwan Constituency hexjson with library(clhex)</li> <li>Draw Taiwan Constituency with hexjson editor according to geographical posistion</li> <li>Import edited hexjson into R and plot with geom_sf()</li> <li>Export the .SVG for further use</li> </ul> </ul> --- class: inverse, center, middle # R time: Hexmap --- # Map: Summary <ul> <li>Plots <ul> <li>背景地圖/Background Map</li> <li>統計地圖/Choropleth</li> <li>點示地圖/Dot Distribution Map; Bubble Map</li> <li>示意地圖/Cartogram</li> <li>國會席次圖/Parliament Plots</li> <li>六邊型網格圖/Hexmap/Tilegram</li> </ul> --- class: inverse, center, middle # 休息一下/Intermission --- # Non-Map: Visualizing NTU Student Congress Vote Rate and Attendance Rate <ul> <li>Plots <ul> <li>Stacked Bar Plots</li> <li>Scatterplot</li> <li>Facet Line Plot</li> </ul> <li>Data <ul> <li>NTU Student Congress</li> <li>Election: Vote Rate</li> <li>Congress: Attedance Rate</li> <li>Variable: Grade, College, Term</li> </ul> </ul> --- # Non-Map: Three Topics <ul> <li>Election: Vote Rate <li>Congress: Attedance Rate <li>Vote Rate & Attendance Rate </ul> --- class: inverse, center, middle # R time --- # Further <ul> <li>Books <ul> <li><a href="https://bookdown.org/robinlovelace/geocompr/adv-map.html" target="_blank">Geocomputation with R </a></li> <li><a href="https://bookdown.org/tpemartin/108-1-ntpu-datavisualization/" target="_blank">ntpu-datavisualization</a></li> </ul> </ul> --- class: inverse, center, middle # Thanks