getwd()
## [1] "C:/data"
setwd("C:/data")
getwd()
## [1] "C:/data"
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
df<-read.csv("ta1.csv",fileEncoding = "euc-kr")

View(df)

names(df)
## [1] "가해자연령층별.1." "월별.1."           "X2022"            
## [4] "X2022.1"           "X2022.2"
df1<-df %>% rename(month=월별.1.,사고건수=X2022,사망지수=X2022.1,부상자수=X2022.2,연령층=가해자연령층별.1.)
View(df1)

head(df1)
##              연령층   month      사고건수      사망지수      부상자수
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
## 2          20세이하    전체          6508            72          8863
## 3          20세이하     1월           435             6           633
## 4          20세이하     2월           357             2           505
## 5          20세이하     3월           473             3           678
## 6          20세이하     4월           580             7           810
df1<-df1 %>% slice(-1)

df2<-df1 %>% filter(month!="전체") %>% glimpse()
## Rows: 108
## Columns: 5
## $ 연령층   <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month    <chr> "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월"…
## $ 사고건수 <chr> "435", "357", "473", "580", "713", "668", "639", "557", "609"…
## $ 사망지수 <chr> "6", "2", "3", "7", "6", "5", "6", "7", "8", "8", "8", "6", "…
## $ 부상자수 <chr> "633", "505", "678", "810", "955", "877", "847", "735", "808"…
df2$사고건수<-as.numeric(df2$사고건수)
glimpse(df2)
## Rows: 108
## Columns: 5
## $ 연령층   <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month    <chr> "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월"…
## $ 사고건수 <dbl> 435, 357, 473, 580, 713, 668, 639, 557, 609, 618, 532, 327, 2…
## $ 사망지수 <chr> "6", "2", "3", "7", "6", "5", "6", "7", "8", "8", "8", "6", "…
## $ 부상자수 <chr> "633", "505", "678", "810", "955", "877", "847", "735", "808"…
library(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country   <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year      <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp   <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop       <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
y<-gapminder %>% group_by(year,continent) %>% summarise(c_pop=sum(pop))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
head(y,20)
## # A tibble: 20 × 3
## # Groups:   year [4]
##     year continent      c_pop
##    <int> <fct>          <dbl>
##  1  1952 Africa     237640501
##  2  1952 Americas   345152446
##  3  1952 Asia      1395357351
##  4  1952 Europe     418120846
##  5  1952 Oceania     10686006
##  6  1957 Africa     264837738
##  7  1957 Americas   386953916
##  8  1957 Asia      1562780599
##  9  1957 Europe     437890351
## 10  1957 Oceania     11941976
## 11  1962 Africa     296516865
## 12  1962 Americas   433270254
## 13  1962 Asia      1696357182
## 14  1962 Europe     460355155
## 15  1962 Oceania     13283518
## 16  1967 Africa     335289489
## 17  1967 Americas   480746623
## 18  1967 Asia      1905662900
## 19  1967 Europe     481178958
## 20  1967 Oceania     14600414
plot(y$year,y$c_pop)

plot(gapminder$gdpPercap,gapminder$lifeExp,col=gapminder$continent)
legend("bottomright",legend = levels((gapminder$continent)),pch=c(1:length(levels(gapminder$continent))),col=c(1:length(levels(y$continent))))

library(ggplot2)

ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,col=continent))+geom_point()+scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,col=continent,size=pop))+geom_point()+scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,col=continent,size=pop))+geom_point(alpha=0.4)+scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,col=continent,size=pop))+geom_point(alpha=0.4)+scale_x_log10()+facet_wrap(~year)

gapminder %>% filter(year==1952&continent=="Asia") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()

gapminder %>% filter(year==1952&continent=="Africa") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()

gapminder %>% filter(year==1952&continent=="Americas") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()

gapminder %>% filter(year==1952&continent=="Europe") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()

gapminder %>% filter(year==1952&continent=="Oceania") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip()

gapminder %>% filter(country=='Korea, Rep.')
## # A tibble: 12 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Korea, Rep. Asia       1952    47.5 20947571     1031.
##  2 Korea, Rep. Asia       1957    52.7 22611552     1488.
##  3 Korea, Rep. Asia       1962    55.3 26420307     1536.
##  4 Korea, Rep. Asia       1967    57.7 30131000     2029.
##  5 Korea, Rep. Asia       1972    62.6 33505000     3031.
##  6 Korea, Rep. Asia       1977    64.8 36436000     4657.
##  7 Korea, Rep. Asia       1982    67.1 39326000     5623.
##  8 Korea, Rep. Asia       1987    69.8 41622000     8533.
##  9 Korea, Rep. Asia       1992    72.2 43805450    12104.
## 10 Korea, Rep. Asia       1997    74.6 46173816    15994.
## 11 Korea, Rep. Asia       2002    77.0 47969150    19234.
## 12 Korea, Rep. Asia       2007    78.6 49044790    23348.
gapminder %>% filter(country=='Korea, Rep.') %>% ggplot(aes(year,lifeExp,col=country))+geom_point()+geom_line()