Date & Time In-class exercise4

dta <- read.table("C:/Users/user/Desktop/birth_college.txt",header=T)
dta$Year <- 1981:2009
dta$Acceptance <- dta$Entrance-40
head(dta)
library(ggplot2)
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
ggplot() +
 geom_point(data=dta, 
            aes(Year, Birth),
            pch=1) +
 geom_point(data=dta, 
            aes(Year, Acceptance),
            pch=16) +
  scale_y_continuous(
name=expression("Birth rate(0.1%)"),limits=c(0,60),breaks = seq(0,60,10),
sec.axis = sec_axis(~.+40,breaks = seq(40,100,10),name="Acceptance rate(%)"))+
  
  
 theme_ipsum()

Map in-class exercise 1

#

#
library(rworldmap)
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
dta <- data.frame(Country=c("Taiwan", "Japan", "USA", "Philippines", "Canada", "Italy", "Vietnam", "France", "Belgium", "Netherland","China"))

library(countrycode)

# a data.frame with the ISO3 country names plus a variable to
# merge to the map data
dta$Country <- countrycode(dta[,1], "country.name", "iso3c")
dta$visited <- rep(1, length(dta$Country))

mapDevice("x11")

# join the data.frame to the country map data
ebolaMap <- joinCountryData2Map(dta, joinCode = "ISO3", nameJoinColumn = "Country")
## 10 codes from your data successfully matched countries in the map
## 1 codes from your data failed to match with a country code in the map
## 233 codes from the map weren't represented in your data
# plot it, the color palette's first color is red
mapCountryData(ebolaMap, nameColumnToPlot = "visited", catMethod = "categorical",
               addLegend = FALSE, mapTitle =" ", missingCountryCol = gray(.9))

#
dev.off()
## png 
##   2
###

Map in-class exercise 2

library(maptools)
## 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()
tw <- readShapePoly("C:/Users/user/Desktop/data/TWN_adm2.shp")
## Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
names(tw)
##  [1] "ID_0"       "ISO"        "NAME_0"     "ID_1"       "NAME_1"    
##  [6] "ID_2"       "NAME_2"     "VARNAME_2"  "NL_NAME_2"  "HASC_2"    
## [11] "CC_2"       "TYPE_2"     "ENGTYPE_2"  "VALIDFR_2"  "VALIDTO_2" 
## [16] "REMARKS_2"  "Shape_Leng" "Shape_Area"
tw$NAME_2
##  [1] Kaohsiung City Taipei City    Changhwa       Chiayi         Hsinchu       
##  [6] Hualien        Ilan           Kaohsiung      Keelung City   Miaoli        
## [11] Nantou         Penghu         Pingtung       Taichung       Taichung City 
## [16] Tainan         Tainan City    Taipei         Taitung        Taoyuan       
## [21] Yunlin        
## 21 Levels: Changhwa Chiayi Hsinchu Hualien Ilan Kaohsiung ... Yunlin
# I have visted all the administratice areas of Taiwan
tw$visited <- c(1,1, 1, 1, 1,1, 1, 
          1, 1, 1, 1, 1, 1, 
          1, 1,1, 1, 1, 1,
          1, 1)

# green or white by whether I've visited there or not
gcol <- ifelse(tw$visited >= 1, "green", "white")

# plot
plot(tw, col=gcol)
legend("topleft", inset=.02, c("have visited", "haven't visited"), fill=c("green","white"))

###

Map in-class exercise 3

library(leaflet)
m <- leaflet() %>%
 addTiles() %>%  
 addMarkers(lng=120.360928, 
            lat=23.1182819, 
            popup="阿江麵店") %>%  
 addMarkers(lng=120.2295947, 
            lat=23.1185553,
            popup="豐咖啡") %>%  
 addMarkers(lng=120.1999312, 
            lat=22.9911496,, 
            popup="COCO壱番屋") 
m