## ─ Attaching packages ────────────────────────── tidyverse 1.3.0 ─
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.3 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ─ Conflicts ─────────────────────────── tidyverse_conflicts() ─
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type : vignette('rworldmap')
## Checking rgeos availability: FALSE
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
## The following object is masked from 'package:tidyr':
##
## extract
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
## rgdal: version: 1.4-8, (SVN revision 845)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: FALSE
## Loaded PROJ.4 runtime: Rel. 5.2.0, September 15th, 2018, [PJ_VERSION: 520]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
## Linking to sp version: 1.4-1
## Loading required package: acs
## Loading required package: XML
##
## Attaching package: 'acs'
## The following object is masked from 'package:dplyr':
##
## combine
## The following object is masked from 'package:base':
##
## apply
In-Class Exercise 1: Countries I Have Visited So Far
dta <- data.frame(Country = c("Taiwan","Japan","South Korea", "Singapore"), Visited = c(1,1)) %>%
mutate(Country = countrycode(Country, "country.name", "iso3c"))
Map <- joinCountryData2Map(dta, joinCode = "ISO3", nameJoinColumn = "Country")## 4 codes from your data successfully matched countries in the map
## 0 codes from your data failed to match with a country code in the map
## 239 codes from the map weren't represented in your data
mapCountryData(Map, nameColumnToPlot = "Visited", catMethod = "categorical",
addLegend = FALSE, mapTitle ="Countries I Have Visited So Far",
missingCountryCol = gray(.9))In-Class Exercise 2: Places in administratice areas of Taiwan you have visited so far
Show the administratice areas of Taiwan
## Reading layer `TWN_adm2' from data source `/Users/haolunfu/Documents/資料管理/week10/TWN_adm/TWN_adm2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 21 features and 18 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 119.3138 ymin: 21.89653 xmax: 122.1085 ymax: 25.63431
## CRS: 4326
## [1] "Kaohsiung City" "Taipei City" "Changhwa" "Chiayi"
## [5] "Hsinchu" "Hualien" "Ilan" "Kaohsiung"
## [9] "Keelung City" "Miaoli" "Nantou" "Penghu"
## [13] "Pingtung" "Taichung" "Taichung City" "Tainan"
## [17] "Tainan City" "Taipei" "Taitung" "Taoyuan"
## [21] "Yunlin"
col = c(rep("lightgray",11),"red",rep("lightgray",9))
plot(tw, col=col, max.plot = 1, main = "Taiwan")
legend("topleft", inset=.02, c("have visited", "haven't visited"), fill=c("lightgray","red"))I haven’t been to off shore of Taiwan.
In-Class Exercise 3: Map an area of Tainan city to include three of your favorite places to eat as landmarks.
Exercise 1:
Load data file
## Reading layer `TWN_adm2' from data source `/Users/haolunfu/Documents/資料管理/week10/TWN_adm/TWN_adm2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 21 features and 18 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 119.3138 ymin: 21.89653 xmax: 122.1085 ymax: 25.63431
## CRS: 4326
## Simple feature collection with 6 features and 18 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 120.1185 ymin: 22.60095 xmax: 121.7513 ymax: 25.13074
## CRS: 4326
## ID_0 ISO NAME_0 ID_1 NAME_1 ID_2 NAME_2 VARNAME_2
## 1 228 TWN Taiwan 3048 Kaohsiung 33637 Kaohsiung City Gaoxiong Shi
## 2 228 TWN Taiwan 3050 Taipei 33638 Taipei City Taibei Shi
## 3 228 TWN Taiwan 3051 Taiwan 33639 Changhwa Zhanghua|Changhua
## 4 228 TWN Taiwan 3051 Taiwan 33640 Chiayi Jiayi|Chiai
## 5 228 TWN Taiwan 3051 Taiwan 33641 Hsinchu Xinzhu
## 6 228 TWN Taiwan 3051 Taiwan 33642 Hualien Hualia
## NL_NAME_2 HASC_2 CC_2 TYPE_2 ENGTYPE_2 VALIDFR_2
## 1 <NA> TW.KH.KC <NA> Chuan-shih Special Municipality 1979
## 2 <NA> TW.TP.TC <NA> Chuan-shih Special Municipality 1967
## 3 <NA> TW.TW.CG <NA> District|Hsien County 1951
## 4 <NA> TW.TW.CH <NA> District|Hsien County 1951
## 5 <NA> TW.TW.HH <NA> District|Hsien County 1951
## 6 <NA> TW.TW.HL <NA> District|Hsien County 1951
## VALIDTO_2 REMARKS_2 Shape_Leng Shape_Area geometry
## 1 Present <NA> 0.5856767 0.006017124 MULTIPOLYGON (((120.239 22....
## 2 Present <NA> 0.6103200 0.023792317 MULTIPOLYGON (((121.5258 24...
## 3 Present <NA> 2.4738100 0.105527533 MULTIPOLYGON (((120.4176 24...
## 4 Present <NA> 3.0710987 0.152489131 MULTIPOLYGON (((120.1526 23...
## 5 Present <NA> 2.0131853 0.155745500 MULTIPOLYGON (((120.9146 24...
## 6 Present <NA> 3.8199624 0.415714667 MULTIPOLYGON (((121.5018 23...
add the obtained votes results
Plot
mcol <- c(RColorBrewer::brewer.pal(3, 'Blues')[3:1],
RColorBrewer::brewer.pal(6, 'Greens'))
brks <- classIntervals(tw$gvotes, n = 9, style = 'quantile')$brks
plot(tw, col = mcol[findInterval(tw$gvotes, brks, all.inside = TRUE)],
axes = FALSE, max.plot = 1, main = '')
legend("topleft", legend = maptools::leglabs(round(brks)),
title = 'Percentage of votes that DPP obtained',
cex=0.8, fill = mcol, bty = 'n', x.intersp = .6, y.intersp = .9)Exercise 3: The data for age fisrt have sex across several countries.
Load data file
## Date X1.1.2005
## 1 Age of first sex- Global 17.3
## 2 Age of first sex- India 19.8
## 3 Age of first sex- Vietnam 19.6
## 4 Age of first sex- Indonesia 19.1
## 5 Age of first sex- Malaysia 19.0
## 6 Age of first sex- Taiwan 18.9
dta_n <- dta %>%
separate(Date, c("no","region"), sep = '-+\\s') %>%
dplyr::select(-no) %>% rename(value = X1.1.2005) %>%
mutate(region = tolower(region))
head(dta_n) ## region value
## 1 global 17.3
## 2 india 19.8
## 3 vietnam 19.6
## 4 indonesia 19.1
## 5 malaysia 19.0
## 6 taiwan 18.9
Plot
country_choropleth(dta_n,
num_colors=9) +
scale_fill_brewer(palette="Blues")+
labs(title="Age When You First Had Sex")## Warning in super$initialize(country.map, user.df): Your data.frame contains the
## following regions which are not mappable: global, hong kong, singapore, serbia &
## montenegro, united states
## Warning in self$bind(): The following regions were missing and are being set
## to NA: afghanistan, angola, azerbaijan, moldova, madagascar, mexico, macedonia,
## mali, myanmar, montenegro, mongolia, mozambique, mauritania, burundi, malawi,
## namibia, niger, nigeria, nicaragua, nepal, oman, pakistan, panama, peru,
## philippines, papua new guinea, united states of america, north korea, benin,
## paraguay, qatar, romania, russia, rwanda, western sahara, saudi arabia, sudan,
## burkina faso, south sudan, senegal, solomon islands, sierra leone, el salvador,
## somaliland, somalia, republic of serbia, suriname, bangladesh, slovenia,
## swaziland, syria, chad, togo, tajikistan, turkmenistan, east timor, trinidad
## and tobago, tunisia, united republic of tanzania, uganda, ukraine, uruguay,
## uzbekistan, the bahamas, venezuela, vanuatu, yemen, zambia, zimbabwe, bosnia
## and herzegovina, belarus, albania, belize, bolivia, brazil, brunei, bhutan,
## botswana, central african republic, united arab emirates, ivory coast, cameroon,
## democratic republic of the congo, republic of congo, colombia, costa rica,
## cuba, northern cyprus, cyprus, argentina, djibouti, dominican republic, algeria,
## ecuador, egypt, eritrea, armenia, estonia, ethiopia, fiji, gabon, georgia,
## ghana, antarctica, guinea, gambia, guinea bissau, equatorial guinea, guatemala,
## guyana, honduras, haiti, hungary, iran, iraq, jamaica, jordan, kazakhstan,
## kenya, kyrgyzstan, cambodia, south korea, kosovo, kuwait, laos, lebanon,
## liberia, libya, sri lanka, lesotho, lithuania, luxembourg, latvia, morocco
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.
Exercise 4: Flood in schools in Taipei
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/haolunfu/Documents/資料管理/week10/flood-in-school/flood50.shp", layer: "flood50"
## with 5103 features
## It has 5 fields
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/haolunfu/Documents/資料管理/week10/flood-in-school/tpecity_school.shp", layer: "tpecity_school"
## with 198 features
## It has 3 fields
## Integer64 fields read as strings: STUDENTS
plot(Flood, col='cyan')
plot(Schools, col='gray', pch=20, add=TRUE)
plot(Schools.Flood, col='red', pch=20, add=TRUE)## TEXTSTRING ID STUDENTS
## 1 士林國中 1 95
## 2 士林國小 2 93
## 8 明德國中 33 113
## 9 文林國小 39 213
## 13 富安國小 48 63
## 26 士東國小 300 52
## 42 大湖國小 681 92
## 52 百齡國小 730 163
## 61 新湖國小 837 24
## 67 民生國中 849 35
## 72 中山國中 863 200
## 73 五常國中 864 91
## 74 五常國小 865 55
## 75 吉林國小 882 91
## 78 劍潭國小 903 204
## 80 蓬萊國小 913 119
## 81 雙蓮國小 915 24
## 82 日新國小 916 201
## 83 民權國中 917 186
## 84 大同國小 918 25
## 87 太平國小 924 91
## 89 大橋國小 926 161
## 91 華江國小 979 99
## 98 龍山國小 1051 87
## 100 仁愛國小 1059 56
## 101 仁愛國中 1060 133
## 106 長春國小 1085 48
## 119 永吉國中 1199 101
## 123 木柵國中 1247 118
## 124 國光劇校 1248 68
## 131 興隆國小 1276 63
## 134 建安國小 1287 43
## 138 忠孝國小 1306 152
## 149 雙園國小 1362 110
## 174 興福國中 1575 85
## 175 溪口國小 1576 168
## [1] 3722