install.packages("ggplot2")
library(ggplot2)
install.packages("readxl")
library(readxl)
install.packages("dplyr")
library(dplyr)
str(sampletext)

sample <- read.csv("도로교통공단_시도_시군구별_가해운전자_연령층별_교통사고(2018).csv")
View(sample)

sampletext <- sample

## 전국 교통사고 가해운전자 총 합
sum(sampletext %>%
  filter(sampletext$연령=="12세이하") %>%
  select(발생건수))  ## 398
sum(sampletext %>%
      filter(sampletext$연령=="13-20세") %>%
      select(발생건수))  ##6256
sum(sampletext %>%
      filter(sampletext$연령=="21-30세") %>%
      select(발생건수))  ## 28727

sum(sampletext %>%
      filter(sampletext$연령=="31-40세") %>%
      select(발생건수)) ## 33618
sum(sampletext %>%
      filter(sampletext$연령=="41-50세") %>%
      select(발생건수))  ## 42204
sum(sampletext %>%
      filter(sampletext$연령=="51-60세") %>%
      select(발생건수))  ## 53631

sum(sampletext %>%
      filter(sampletext$연령=="61-64세") %>%
      select(발생건수))  ## 18095
sum(sampletext %>%
      filter(sampletext$연령=="65세이상") %>%
      select(발생건수))  ## 30012

sum(sampletext %>%
      filter(sampletext$연령=="불명") %>%
      select(발생건수))  ## 4207

sum(sampletext$발생건수)  ## 217148

sampletext_sum <- bind_rows(sampletext, loadkill12)

View(sampletext_sum)
View(final_a)
sampletext$가해자총합 <- NA
sampletext

## 서울에서 발생한 교통사고
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="12세이하") %>%
      select(발생건수)) ## 95
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="13-20세") %>%
      select(발생건수)) ##1137
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="21-30세") %>%
      select(발생건수))  ## 4720

sum(sampletext %>%
      filter(시도 =="서울" & 연령=="31-40세") %>%
      select(발생건수)) ## 6021
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="41-50세") %>%
      select(발생건수))  ## 6773
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="51-60세") %>%
      select(발생건수))  ## 9356

sum(sampletext %>%
      filter(시도 =="서울" & 연령=="61-64세") %>%
      select(발생건수))  ## 3796

sum(sampletext %>%
      filter(시도 =="서울" & 연령=="65세이상") %>%
      select(발생건수))  ## 5869
sum(sampletext %>%
      filter(시도 =="서울" & 연령=="불명") %>%
      select(발생건수))  ## 1028
sum(
  sampletext %>% 
    filter(시도 =="서울") %>%
    select(발생건수)) ##38795

ggplot(data = final_a, aes(x = 연령,y = 발생건수)) + geom_bar()



maptest<- data.frame(시도= "서울",
                    code="11",
                          sum=1028)

sampletext_sum <- bind_rows(sampletext_sum, loadkill12)

View(sampletext_sum)
sampletext_sum <-final_a 
final_a <- sampletext_sum

View(final_a)
geomtest <- final_a %>% 
  filter(시도 == "전국" & 연령 != "총합")
geomtest
a <- ggplot(data = geomtest, aes(x = 연령, y =가해자총합)) +geom_col()
install.packages("plotly")
library(plotly)
ggplotly(a)

final_a %>% filter(시도 =="전국" & 연령 =="총합") %>% 
  select(가해자총합)
final_a %>% filter(시도 =="서울" & 연령 =="총합") %>% 
  select(가해자총합)

final_b <- final_a %>% filter(시도 =="서울" & 연령 =="총합") %>% 
               select(가해자총합)
final_c <-final_a %>% filter(시도 =="전국" & 연령 =="총합") %>% 
  select(가해자총합)
final_b
final_c

final_d <- (final_b$가해자총합/final_c$가해자총합)*100
final_d


final_e
View(final_e)
abc <- final_a %>%  
  filter(final_a$시도 =="서울" & 연령 =="12세이하") %>% 
  select(가해자총합) %>% 
  filter(!is.na(가해자총합))

def <- final_a %>%  
  filter(final_a$시도 =="서울" & 연령 =="총합") %>% 
  select(가해자총합) %>% 
  filter(!is.na(가해자총합))
abc/def *100

abc
def



sum(
  sampletext %>% 
    filter(시도 =="서울") %>%
    select(발생건수)) ##38795 ## 11

sum(
  sampletext %>% 
    filter(시도 =="경기") %>%
    select(발생건수)) ## 53448  ##31

sum(
  sampletext %>% 
    filter(시도 =="인천") %>%
    select(발생건수)) ##7632  ##23

sum(
  sampletext %>% 
    filter(시도 =="강원") %>%
    select(발생건수)) ## 7498  ## 32

sum(
  sampletext %>% 
    filter(시도 =="충북") %>%
    select(발생건수)) ## 9618  ## 33

sum(
  sampletext %>% 
    filter(시도 =="대전") %>%
    select(발생건수)) ## 7554  ## 25

sum(
  sampletext %>% 
    filter(시도 =="세종") %>%
    select(발생건수))  ## 795  ## 29

sum(
  sampletext %>% 
    filter(시도 =="충남") %>%
    select(발생건수)) ##8807  ## 34

sum(
  sampletext %>% 
    filter(시도 =="부산") %>%
    select(발생건수)) ## 11937  ## 21

sum(
  sampletext %>% 
    filter(시도 =="전북") %>%
    select(발생건수)) ##6929  ## 35

sum(
  sampletext %>% 
    filter(시도 =="전남") %>%
    select(발생건수)) ## 9787  ## 36

sum(
  sampletext %>% 
    filter(시도 =="경북") %>%
    select(발생건수))  ## 13966  ## 37

sum(
  sampletext %>% 
    filter(시도 =="경남") %>%
    select(발생건수)) ## 11493  ## 38

sum(
  sampletext %>% 
    filter(시도 =="제주") %>%
    select(발생건수)) ## 4239  ## 39

sum(
  sampletext %>% 
    filter(시도 =="대구") %>%
    select(발생건수)) ## 13199  ## 22

sum(
  sampletext %>% 
    filter(시도 =="광주") %>%
    select(발생건수)) ## 7459  ## 24

sum(
  sampletext %>% 
    filter(시도 =="울산") %>%
    select(발생건수)) ## 3992  ## 26


maptest1<- data.frame(시도= "울산",
                        code="26",
                        sum=3992)
maptest <- bind_rows(maptest, maptest1)

maptest
maptest_a <- maptest
maptest_a <- rename(maptest_a,
                    name = 시도)
maptest_a


ggChoropleth(data = maptest_a,
             aes(fill =sum,
                 map_id = code),
             map =kormap1,
             interactive =T)
