Introduction

We crawl venues where women and childen crime cases generally happen in the first half of 2017 in Taiwan from open data in MOI and also summarize the number of venues by country. Furthermore, we also crawl venues of police stations and list their basic information in the map along with the crime scenes. It is expected that this report can do positive constribution to our social security.

Crawling Crime Scenes and Ploting Point Graph by Country

We first crawl the data from MOI and obtain longituds and latitudes of crime scenes from Google Maps API. We also fetch country as a separate column.

library(ggmap)
injuryurl <- "http://data.gov.tw/iisi/logaccess/78805?dataUrl=http://data.moi.gov.tw/MoiOD/System/DownloadFile.aspx?DATA=CDBDB9E4-B2EB-47FC-9B1F-01941514AFAC&ndctype=CSV&ndcnid=6247"
download.file(injuryurl, destfile = "WomenChildrenInjury.csv")
injury <- read.csv("WomenChildrenInjury.csv")

injury <- injury[-c(1),]

index <-1
for (str in injury$Address){
    country <- substr(str, start = 1, stop = 3)
    if (identical(as.character(country),"花蓮火")){
        country <- "花蓮縣"
    }
    if (identical(as.character(country),"花蓮市")){
        country <- "花蓮縣"
    }
    if (identical(as.character(country),"澎湖水")){
        country <- "澎湖縣"
    }
    if (identical(as.character(country),"臺南式")){
        country <- "臺南市"
    }
    injury$country[index] <- country
    loc <-geocode(str)
    injury$longitud[index] <- loc$lon
    injury$latitude[index] <- loc$lat 
    index <- index+1
}

Since we have processed the above process, we just load the file saved before.

injury = read.csv("injury.csv")

We rearrange countries from north to south:

countrylist <- as.data.frame(table(injury$country))
countrylist <- countrylist[c(3, 10, 15, 14, 17, 16, 6, 13, 20, 7, 19, 5, 4, 12, 1, 9, 18, 2, 11, 8),]

The total number of crime scenes is

nrow(injury)
## [1] 393

And we can list the detail, save data as csv file, and draw a point graph by country:

countrylist
##      Var1 Freq
## 3  基隆市   22
## 10 臺北市    4
## 15 新北市   28
## 14 桃園市   38
## 17 新竹縣   15
## 16 新竹市   11
## 6  苗栗縣   32
## 13 臺中市   13
## 20 彰化縣   24
## 7  南投縣   25
## 19 雲林縣   28
## 5  嘉義縣   39
## 4  嘉義市    4
## 12 臺南市   31
## 1  高雄市   16
## 9  屏東縣   17
## 18 宜蘭縣   16
## 2  花蓮縣   16
## 11 臺東縣   10
## 8  澎湖縣    4
library(ggthemes)
library(ggplot2)
par(family='STHeiti')

ggplot(data = countrylist, aes(x = Var1, y = Freq)) + geom_point() +  theme_tufte() + xlab("Country where Crime Scenes Happen") +ylab("Number of Venues") + theme_gray(base_family="STHeiti") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    ggtitle("Venues Where Women and Children Injury Cases Happen") + 
    scale_x_discrete(limits = c("基隆市", "臺北市", "新北市", "桃園市", "新竹縣", "新竹市", "苗栗縣", "臺中市", "彰化縣", "南投縣", "雲林縣", "嘉義縣", "嘉義市", "臺南市", "高雄市", "屏東縣", "宜蘭縣", "花蓮縣", "臺東縣", "澎湖縣"))

Crawling Police Stations

Similarity, we download the data from MOI and obtain longituds and latitudes of stations from Google Maps API.

police <- read.csv("police.csv")

index <-1
for (str in police$地址){
    loc <-geocode(str)
    police$longitud[index] <- loc$lon
    police$latitude[index] <- loc$lat 
    index <- index +1
}

Also, since we have processed the above process, we just load the file saved before.

police <- read.csv("police2.csv")

The total number of police stations in Taiwan is:

nrow(police)
## [1] 1694

Making up pop-ups and Drawing Map

We then make up pop-ups for crime scenes and police stations in order to present in the map.

library(dplyr)
injury <- injury %>%
    mutate(popup_info = paste("<b>Address:</b>", Address, "<br />"))

police <- police %>%
    mutate(popup_info = paste("<b>Name:</b>", 中文單位名稱, "<br />",
                              "<b>Address:</b>", 地址, "<br />",
                              "<b>Phone num:</b>", 電話, "<br />"))

Finally, we draw the map to show our results.

library(leaflet)
leaflet() %>%
    addTiles() %>%
    addMarkers(data = injury,
               lng = ~ longitud, lat = ~ latitude,
               popup = ~popup_info, clusterOptions = markerClusterOptions()) %>%
    addCircleMarkers(data = police, radius = 5, 
                lng = ~longitud, lat = ~ latitude, opacity = 0.9,
                popup = ~popup_info)