Maps in class exercise & Homework
In Class Exercise
Exercise 1
Indicate countries you have visited so far on a world map in the style of the ebola outbreaks example.
pacman::p_load(maps, mapdata, maptools, mapproj, rworldmap, rgeos,
raster, sp, sf, rgdal, choroplethr, choroplethrMaps,
RColorBrewer, countrycode, classInt, scales, tidyverse)library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
world$visit <-ifelse(world$name_sort%in%
c("Hong Kong SAR, China", "Japan", "Macao SAR, China", "Mexico", "Philippines",
"Singapore" ,"Solomon Islands", "Taiwan","Vietnam",
"South Africa", "Thailand", "United States of America",
"Netherlands", "Australia", "United Arab Emirates"), "1", "0")
ggplot(data = world) +
geom_sf(aes(fill = visit))+
scale_fill_manual(values=c("white", "gray48"))+
ggtitle("Countries I have been to")+
theme(legend.position = "none", plot.title=element_text(hjust=0.5))Exercise 2
Plot places in administratice areas of Taiwan you have visited so far.
library(ggplot2)
library(sf)
taiwan.map <- st_read("C:/Users/USER/Desktop/R_data management/0518/COUNTY_MOI_1081121.shp")## Reading layer `COUNTY_MOI_1081121' from data source `C:\Users\USER\Desktop\R_data management\0518\COUNTY_MOI_1081121.shp' using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 114.3593 ymin: 10.37135 xmax: 124.5611 ymax: 26.38528
## geographic CRS: TWD97
taiwan.map$visit<-ifelse(taiwan.map$COUNTYENG%in%c("Lienchiang County", "Kinmen County", "Penghu County"), 0,1)
ggplot() +
geom_sf() +
geom_sf(data = taiwan.map, aes(fill = as.character(visit))) +
scale_fill_discrete(name = " ", labels=c("haven't visited", "visited"))+
geom_path() +
coord_sf(ylim = c(20, 30), expand = FALSE)Exercise 3
Map an area of Tainan city to include three of your favorite places to eat as landmarks.
Exercise
Exercise 1
Build a thematic plot of the results of Taiwan 2020 presidential election between the DDP and the KMT. The geographical data (maps) for Taiwan can be obtained from DIVA-GIS: Geographic Information System for Biodiversity Research. Source: Taiwan presidential election 2020 . Wikipedia.
library(maptools)
library(classInt)
library(dplyr)
library(ggplot2)
tw<-st_read("C:/Users/USER/Desktop/R_data management/0518/TWN_adm2.shp")## Reading layer `TWN_adm2' from data source `C:\Users\USER\Desktop\R_data management\0518\TWN_adm2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 11 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 116.71 ymin: 20.6975 xmax: 122.1085 ymax: 25.63431
## geographic CRS: WGS 84
## [1] "ID_0" "ISO" "NAME_0" "ID_1" "NAME_1" "ID_2"
## [7] "NAME_2" "TYPE_2" "ENGTYPE_2" "NL_NAME_2" "VARNAME_2" "geometry"
# names of administrative unit
# percents votes received by the DDP party
# hand code data from wiki
# order by names of administrative unit
# attach the data to map data
tw$gvotes <- c(62.23,NA,53.65,57.17,64.22,
56.52,35.91,63.28,62.23,
50.82,45.02,50.83,53.85,
62.16,56.95,56.95,67.38,
67.38,56.52,38.12,54.78,
61.56)
# green or blue by 50 percent votes
gcol <- ifelse(tw$gvotes > 50.0, "green", "blue")
# plot
plot(tw, col=gcol) ##
# second plot #
# generate color scheme using the palette function
mcol <- brewer.pal(7, "Greens")
#set breaks for the 7 colors
brks <- classIntervals(na.omit(tw$gvotes, n=7, style="quantile"))
brks <- brks$brks
#plot the map
ggplot() +
geom_sf() +
geom_sf(data = tw, aes(fill = as.factor(findInterval(gvotes, brks, all.inside = TRUE))))+
scale_fill_manual(name = " ", labels=c("under 50", "50-54","54-57", "57-62",
"62-63", ">63"), values=brewer.pal(7, "Greens"))+
ggtitle("Taiwan Presidential Election, 2020")+
theme(legend.position =c(0.2,0.7), plot.title=element_text(hjust=0.5))Exercise 2 The link for exercise 2 is not available.
Traffic accidents on roads in Taiwan in 2011 is available on-line from the Department of Transportation. Plot the number of deaths per 10,000 vehicles over the administrative units.
Exercise 3
Download the data for age fisrt have sex across several countries to make the following plot:…
library(choroplethr)
library(dplyr)
dta<-read.csv("C:/Users/USER/Desktop/R_data management/0518/Age_When_You_First_Had_Sex.csv")%>%
separate(Date, c("title","region"), sep = '-+\\s')%>%dplyr::select(-title)%>%
rename(value = X1.1.2005)%>%
mutate(region = tolower(region))
country_choropleth(dta,
num_colors=8, legend="age") +
scale_fill_brewer(palette='Spectral')+
labs(title="Average age at first sex by Country",
subtitle="2015")Exercise 4
Download all the files from github (click the downward triangle in the clone or download button in green) for flood in schools in Taipei to replicate the analysis with the markdown file included.
降雨頻率分析與淹水潛勢是洪災分析重要的資料來源,利用水文分析進行降雨觀測及淹水分析結果,進一步整合社會統計資料,可評估人類社會承受災害的潛在風險。
假設將24小時延時200年重現期降雨的淹水潛勢的深度達50cm以上的區域,定義為「潛在受災區」。
library(rgdal)
library(sp)
Flood<- rgdal::readOGR("C:/Users/USER/Desktop/R_data management/0518/flood-in-school-master/flood50.shp", encoding = "UTF-8") # encoding="UTF-8## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\USER\Desktop\R_data management\0518\flood-in-school-master\flood50.shp", layer: "flood50"
## with 5103 features
## It has 5 fields
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\USER\Desktop\R_data management\0518\flood-in-school-master\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
## Factor w/ 127 levels "101","102","103",..: 123 122 7 68 101 94 121 35 74 85 ...
## [1] 2645