getwd()
## [1] "C:/data"
setwd("C:/data")
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
list.files("C:/data")
##  [1] "Data1.csv"     "Data1.txt"     "Data1.xls"     "Day1.html"    
##  [5] "Day1.Rmd"      "Day2.Rmd"      "mtcars.csv"    "mypcprotector"
##  [9] "period1.xlsx"  "rsconnect"     "ta1.csv"
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
df <- read.csv("ta1.csv",fileEncoding = "euc-kr")  ### 한글변수명을 읽기 위한 옵션
View(df)
names(df)
## [1] "가해자연령층별.1." "월별.1."           "X2022"            
## [4] "X2022.1"           "X2022.2"
df <- df %>% rename(연령층="가해자연령층별.1.",month="월별.1.",사고건수="X2022",사망자수="X2022.1",부상자수="X2022.2")
names(df)
## [1] "연령층"   "month"    "사고건수" "사망자수" "부상자수"
View(df)
df[1,]
##              연령층   month      사고건수      사망자수      부상자수
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
df1 <- df %>% slice(-1,)
View(df1)
dim(df)
## [1] 118   5
dim(df1)
## [1] 117   5
table(df1$month)
## 
## 10월 11월 12월  1월  2월  3월  4월  5월  6월  7월  8월  9월 전체 
##    9    9    9    9    9    9    9    9    9    9    9    9    9
table(df1$연령층)
## 
## 20세이하  21-30세  31-40세  41-50세  51-60세  61-64세  65-70세 71세이상 
##       13       13       13       13       13       13       13       13 
##     불명 
##       13
table(df1$사고건수)
## 
##  1018  1042  1074  1107  1112  1211  1234  1257  1296  1306  1324  1395  1443 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  1453  1483  1494 15253  1535  1554  1614  1633  1647  1681  1687  1691  1703 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  1705   172  1724  1729  1735  1751  1777  1787  1792   183  1855 19160 19399 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  1945  1950  1960  1969  2001   211   215   216  2182  2197  2228   224  2268 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2276  2295  2307   231  2310  2322   233  2330  2336  2341  2368  2376  2391 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2409   243  2463   247  2489  2503  2525  2589   259  2682 26904  2714  2760 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2776   280  2805 28402  2849  2900  2930  2939  2994  3044  3083  3096  3191 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##   327 33915   357  3677  3683  3733  3747  3772  3872  3911  3989  4001  4156 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##   435 44581   473   532   557   580   609   618   639  6508   668   713   845 
##     1     1     1     1     1     1     1     1     1     1     1     1     1
table(df1$사망자수)
## 
##   0  14  16  17  19   2  20  22  23  24  25  26  27  28  29 297   3  30  31 317 
##  13   1   2   2   3   1   2   4   4   1   2   6   4   3   6   1   1   5   3   1 
##  32  33 335  34 349  37  38 386  39  40 409  41  42  43  44  47  49   5  50  52 
##   3   2   1   5   1   5   3   1   1   1   1   1   2   3   2   2   1   1   1   1 
##  53  56 570   6  61   7  72   8 
##   1   1   1   4   1   2   1   3
table(df1$부상자수)
## 
##  1148  1393  1453  1478  1578  1658  1660  1687  1740  1828  1865  1877   191 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  1973  1994  2013   204  2095 21489  2178  2229  2309   231   233  2333  2339 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2386  2396   242  2430  2432  2464  2472  2477  2503  2512  2528  2542   261 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2613  2614   267  2679   275 27566  2769 27792   278  2793  2816  2820   290 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##  2903   302   305  3079  3086  3209  3285  3302  3382  3418  3419  3428  3442 
##     1     1     1     1     1     2     1     1     1     1     1     1     1 
##  3443  3446  3460  3470  3534  3546  3579  3613  3615  3719  3750  3878  3896 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 39706  4013  4056  4085 41287  4156  4182  4255  4296  4301  4321  4345  4543 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## 48389   486   505  5243  5266  5385  5400  5414  5485  5554  5627  5745  6083 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
##   633 63632   678   724   735   805   808   810   847   877  8863   955 
##     1     1     1     1     1     1     1     1     1     1     1     1
df2 <- df1 %>% filter(df1$month!="전체")
table(df2$month)
## 
## 10월 11월 12월  1월  2월  3월  4월  5월  6월  7월  8월  9월 
##    9    9    9    9    9    9    9    9    9    9    9    9
df2 %>% 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$사고건수)
df2$사망자수<-as.numeric(df2$사망자수)
df2$부상자수<-as.numeric(df2$부상자수)
df2 %>% glimpse()
## 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…
## $ 사망자수 <dbl> 6, 2, 3, 7, 6, 5, 6, 7, 8, 8, 8, 6, 31, 22, 26, 16, 29, 29, 3…
## $ 부상자수 <dbl> 633, 505, 678, 810, 955, 877, 847, 735, 808, 805, 724, 486, 3…
######################################################################################################################

##### 시각화 ggplot2 #####

######################################################################################################################
library(ggplot2)
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) %>% summarize(c_pop=sum(pop))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
y
## # A tibble: 60 × 3
## # Groups:   year [12]
##     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
## # ℹ 50 more rows
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:1:length(levels(gapminder$continent))))

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

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

ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent,size=pop))+geom_point(alpha=0.5)+scale_x_log10()  # alpha = 투명도

ggplot(gapminder,aes(x=gdpPercap,y=lifeExp,col=continent,shape=continent,size=pop))+geom_point(alpha=0.5)+scale_x_log10()+facet_wrap(~year)  # facet_wrap() = 연도별 데이터 확인 가능

gapminder %>% filter(year==1952&continent=="Asia") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip() # coord_flpi = x축과 y축 바꾸기

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

gapminder %>% filter(year==1952&continent=="Oceania") %>% ggplot(aes(reorder(country,pop),pop))+geom_bar(stat="identity")+coord_flip() # coord_flpi = x축과 y축 바꾸기

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

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.