먼저 read 함수를 이용해서 csv 파일을 Rstudio 에 불러온다. csv의 파일명은 boom로 정해준다.
boom <- read.csv(file = "C:/Users/hazard/data/Rclass/Hazard/boom.csv.csv", header = TRUE)
str(boom)
## 'data.frame': 386 obs. of 15 variables:
## $ 다발지식별자 : int 6411461 6411839 6411840 6411836 6411835 6411837 6411193 6411838 6411295 6411841 ...
## $ 다발지그룹식별자: int 2018032 2018032 2018032 2018032 2018032 2018032 2018032 2018032 2018032 2018032 ...
## $ 법정동코드 : num 1.13e+09 1.13e+09 1.13e+09 1.13e+09 1.13e+09 ...
## $ 스팟코드 : int 11305001 11305002 11305003 11305004 11305005 11305006 11305007 11305008 11305009 11320001 ...
## $ 관할경찰서 : Factor w/ 386 levels "강원도 원주시1",..: 218 219 220 221 222 223 224 225 226 249 ...
## $ 다발지명 : Factor w/ 386 levels "강원도 원주시 개운동(향교사거리 부근)",..: 225 224 220 219 226 223 221 222 218 252 ...
## $ 발생건수 : int 6 6 5 5 4 4 4 4 4 7 ...
## $ 사상자수 : int 6 7 6 5 7 5 4 5 6 7 ...
## $ 사망자수 : int 0 0 0 0 0 0 0 0 0 0 ...
## $ 중상자수 : int 3 1 0 0 3 2 2 1 0 4 ...
## $ 경상자수 : int 3 6 6 4 4 2 2 4 6 3 ...
## $ 부상신고자수 : int 0 0 0 1 0 1 0 0 0 0 ...
## $ 경도 : num 127 127 127 127 127 ...
## $ 위도 : num 37.6 37.6 37.6 37.6 37.6 ...
## $ 다발지역폴리곤 : Factor w/ 386 levels "{\"type\":\"Polygon\",\"coordinates\":[[[126.46163742,36.78196310],[126.46162677,36.78212014],[126.46159915,36."| __truncated__,..: 140 135 147 141 137 143 152 138 144 162 ...
boom$경도 <- NULL
boom$위도 <- NULL
boom$다발지역폴리곤<- NULL
boom$다발지식별자 <-NULL
boom$다발지그룹식별자 <-NULL
boom$법정동코드<-NULL
boom$관할경찰서 <-NULL
boom$스팟코드 <-NULL
head(boom)
table(boom$사상자수)
##
## 3 4 5 6 7 8 9 10 11 12
## 5 132 113 65 34 20 7 3 5 2
tab.boom<-table(boom$사상자수)
pie(tab.boom)
barplot(tab.boom)
install.packages( c("ggplot2") ,
repos = "https://cran.seoul.go.kr" )
## Installing package into 'C:/Program Files/R/R-3.5.1/library'
## (as 'lib' is unspecified)
## package 'ggplot2' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Temp\Rtmp844i1w\downloaded_packages
library( ggplot2 )
qplot()
summary(boom)
## 다발지명 발생건수
## 강원도 원주시 개운동(향교사거리 부근) : 1 Min. : 3.000
## 강원도 원주시 원동(천주교원주교구청 부근) : 1 1st Qu.: 4.000
## 강원도 원주시 학성동(우산철교사거리 부근) : 1 Median : 5.000
## 강원도 춘천시 우두동(사우사거리 부근) : 1 Mean : 5.096
## 강원도 춘천시 효자동(가람약국 부근) : 1 3rd Qu.: 6.000
## 경기도 고양시 덕양구 행신동(가라뫼사거리 부근): 1 Max. :12.000
## (Other) :380
## 사상자수 사망자수 중상자수 경상자수
## Min. : 3.000 Min. :0.00000 Min. :0.000 Min. : 0.000
## 1st Qu.: 4.000 1st Qu.:0.00000 1st Qu.:1.000 1st Qu.: 2.000
## Median : 5.000 Median :0.00000 Median :2.000 Median : 3.000
## Mean : 5.358 Mean :0.09845 Mean :1.689 Mean : 2.894
## 3rd Qu.: 6.000 3rd Qu.:0.00000 3rd Qu.:2.000 3rd Qu.: 4.000
## Max. :12.000 Max. :2.00000 Max. :5.000 Max. :10.000
##
## 부상신고자수
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.6762
## 3rd Qu.:1.0000
## Max. :5.0000
##
summary 함수를 사용하면 발생건수의 평균과 사상자수의 평균이 나오게 되는데 , 발생건수의 평균은 5.096, 사상자수의 평균은 5.358 정도가 나왔다.
ggplot(boom, aes(x=발생건수)) + geom_bar(aes(fill=사상자수))
**geom *함수를 이용한 그래프 그리기
g1<- ggplot(data=boom,aes(x=발생건수,y=사상자수))
g1+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 5
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 5
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
geom 그래프로 알수 있는 것
-발생건수가 높을수록 사상자수도 급격히 증가하는 것이 확연히 보인다.
발생건수 대비 사망자수 나타내기
library(ggplot2)
g2<-ggplot(data=boom,aes(x=발생건수,y=사망자수))
g2+geom_point()+geom_smooth()+labs(title="발생건수 대비 사망자수",x="발생건수",y="사망자수")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 5
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 5
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
발생건수에 대해 사망자수는 발생건수가 늘어나도 사망자수는 현저히 줄어들었다
library(ggplot2)
g3<-ggplot(boom,aes(x=발생건수,y=부상신고자수,color="green"))
g3+geom_point()+geom_smooth()+labs(title="발생건수에 따른 중상자수",x="발생건수",y="중상자")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : pseudoinverse used at 5
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : neighborhood radius 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
## parametric, : reciprocal condition number 0
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
## at 5
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
## 1
## Warning in predLoess(object$y, object$x, newx = if
## (is.null(newdata)) object$x else if (is.data.frame(newdata))
## as.matrix(model.frame(delete.response(terms(object)), : reciprocal
## condition number 0
**발생건수에 대비해서 중상자수는 발생건수가 적었을때 보다 클때 중상자수가 많다는 것을 그래프를 통해 알 수 있다.
-csv 파일을 다운로드 받은 사이트를 삽입할 수 있다.
<https://www.data.go.kr/dataset/15003493/fileData.do> <공공데이터포털>