기대 수명 계산

ls()
##  [1] "age"            "d.shape"        "dx"             "dx.f"          
##  [5] "dx.m"           "dx.o"           "e0x"            "e0x.o"         
##  [9] "g1"             "g2"             "g3"             "graunt"        
## [13] "graunt.melt"    "graunt.poly"    "graunt.us"      "graunt.x"      
## [17] "graunt.y"       "halley"         "lifetable"      "lifetable.kr"  
## [21] "lifetable.kr.x" "lifetable.kr.y" "lx"             "mux"           
## [25] "mux.o"          "p.shape"        "p1"             "p2"            
## [29] "p2.shape"       "p3"             "p3.shape"       "p4"            
## [33] "qx"             "qx.f"           "qx.m"           "qx.o"          
## [37] "us.93"          "us.y"           "x"              "y"
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")

2. x-축과 y-축에 관찰된 연령과 생존률 표시.
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th)

3. 0세, 76세를 알아보기 쉽게 점선으로 표시
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)

# graunt.x.2<-c(graunt$x, rev(graunt$x))
# graunt.y.2<-c(rep(0, length(graunt$x)), rev(graunt$lx.17th))
(graunt.x <- c(graunt$x, graunt$x[1]))
##  [1]  0  6 16 26 36 46 56 66 76  0
(graunt.y <- c(graunt$lx.17th, 0))
##  [1] 100  64  40  25  16  10   6   3   1   0
4. 빗금칠.
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
polygon(graunt.x, graunt.y,density=15)

5. 알아보기 쉽게 격자 설정
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
polygon(graunt.x, graunt.y,density=15)
abline(v=graunt$x, lty=2)

6. 제목과 x-축, y-축의 라벨 입력.
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
polygon(graunt.x, graunt.y,density=15)
abline(v=graunt$x, lty=2)
title(main="John Graunt의 생존 곡선", xlab="연령(세)", ylab="생존률(%)")

area.R <- function(x, y) {
  sum(diff(x) * (head(y, -1) + tail(y, -1))/2)
  }
area.R(graunt$x, graunt$lx.17th)/100
## [1] 18.17

1993년 미국의 생명표와 비교

plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)

2. 1993년 미국의 생존함수 추가
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")

3. 1993년 미국인의 76세까지 생존률 70%를 점선으로 표시. 
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")
abline(h=70, lty=2)

4. 70% 눈금에 `las=1`을 써서 수평하게 표시. 
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")
abline(h=70, lty=2)
axis(side=2, at=70, labels=70, las=1)

graunt.x<-c(graunt$x, rev(graunt$x))
graunt.y<-c(rep(0, length(graunt$x)), rev(graunt$lx.17th))
us.y <- c(graunt$lx, rev(us.93$lx.93))
5. 두 곡선의 사이를 빗금으로 표시.
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")
abline(h=70, lty=2)
axis(side=2, at=70, labels=70, las=1)
polygon(graunt.x, us.y, density=15, col="red", border=NA)

6. 알아보기 쉽게 격자 표시
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")
abline(h=70, lty=2)
axis(side=2, at=70, labels=70, las=1)
polygon(graunt.x, us.y, density=15, col="red", border=NA)
abline(v=graunt$x,lty=2)

7. 제목과 x-축, y-축의 라벨 입력.
plot(graunt$x, graunt$lx.17th, ann=F, xaxt="n", yaxt="n", type="b")
axis(side=1, at=graunt$x, labels=graunt$x)
axis(side=2, at=graunt$lx, labels=graunt$lx.17th)
abline(v=c(0, 76), lty=2)
lines(us.93$x, us.93$lx.93, type="b")
abline(h=70, lty=2)
axis(side=2, at=70, labels=70, las=1)
polygon(graunt.x, us.y, density=15, col="red", border=NA)
abline(v=graunt$x,lty=2)
title(main="Graunt와 1993년 미국의 생존 곡선", xlab="연령(세)", ylab="생존률(%)")

area.R(us.93$x, us.93$lx.93)/100
## [1] 70.92
area.R(us.93$x, us.93$lx.93)/100 - area.R(graunt$x, graunt$lx.17th)/100
## [1] 52.75

Halley의 lifetable과 비교

halley
##    age   lx    px
## 1    0 1238 100.0
## 2    1 1000  80.8
## 3    2  855  69.1
## 4    3  798  64.5
## 5    4  760  61.4
## 6    5  732  59.1
## 7    6  710  57.4
## 8    7  692  55.9
## 9    8  680  54.9
## 10   9  670  54.1
## 11  10  661  53.4
## 12  11  653  52.7
## 13  12  646  52.2
## 14  13  640  51.7
## 15  14  634  51.2
## 16  15  628  50.7
## 17  16  622  50.2
## 18  17  616  49.8
## 19  18  610  49.3
## 20  19  604  48.8
## 21  20  598  48.3
## 22  21  592  47.8
## 23  22  586  47.3
## 24  23  579  46.8
## 25  24  573  46.3
## 26  25  567  45.8
## 27  26  560  45.2
## 28  27  553  44.7
## 29  28  546  44.1
## 30  29  539  43.5
## 31  30  531  42.9
## 32  31  523  42.2
## 33  32  515  41.6
## 34  33  507  41.0
## 35  34  499  40.3
## 36  35  490  39.6
## 37  36  481  38.9
## 38  37  472  38.1
## 39  38  463  37.4
## 40  39  454  36.7
## 41  40  445  35.9
## 42  41  436  35.2
## 43  42  427  34.5
## 44  43  417  33.7
## 45  44  407  32.9
## 46  45  397  32.1
## 47  46  387  31.3
## 48  47  377  30.5
## 49  48  367  29.6
## 50  49  357  28.8
## 51  50  346  27.9
## 52  51  335  27.1
## 53  52  324  26.2
## 54  53  313  25.3
## 55  54  302  24.4
## 56  55  292  23.6
## 57  56  282  22.8
## 58  57  272  22.0
## 59  58  262  21.2
## 60  59  252  20.4
## 61  60  242  19.5
## 62  61  232  18.7
## 63  62  222  17.9
## 64  63  212  17.1
## 65  64  202  16.3
## 66  65  192  15.5
## 67  66  182  14.7
## 68  67  172  13.9
## 69  68  162  13.1
## 70  69  152  12.3
## 71  70  142  11.5
## 72  71  131  10.6
## 73  72  120   9.7
## 74  73  109   8.8
## 75  74   98   7.9
## 76  75   88   7.1
## 77  76   78   6.3
## 78  77   68   5.5
## 79  78   58   4.7
## 80  79   50   4.0
## 81  80   41   3.3
## 82  81   34   2.7
## 83  82   28   2.3
## 84  83   23   1.9
## 85  84   20   1.6
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")

2. Halley의 생존곡선에 Graunt의 생명표에 나오는 연령에서 관찰되는 생존률을 같은 모양의 점으로 표시한다.
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)

3. Graunt의 생존곡선을 추가한다. 
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")

4. x-축과 y-축에 라벨을 넣는다. y축 라벨을 보기 쉽게 `las=1` 적용.
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)

5. 0세, 76세, 85세에 점선으로 수직선 설치
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)
abline(v=c(0, 76, 85), lty=2)

5. 적절한 좌표에 각 생존함수의 개발자를 표시하여 구분.
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)
abline(v=c(0, 76, 85), lty=2)
text(x = c(16, 36), y=c(20, 50), label=c("Graunt", "Halley"))

6. 전체 제목과 x-축 및 y-축의 라벨 표시.
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)
abline(v=c(0, 76, 85), lty=2)
text(x = c(16, 36), y=c(20, 50), label=c("Graunt", "Halley"))
title(main="Graunt와 Halley의 생존 곡선 비교", xlab="연령(세)", ylab="생존률(%)")

poly.1.x<-c(0:10, 10.8, 6, 0)
poly.1.y<-c(halley$px[1:11], 52.8, graunt$lx.17th[2:1])
* 교점 아랫부분의 좌표
poly.2.x <- c(10.8, graunt$x[3:9], rev(halley$age[12:85]))
poly.2.y <- c(52.8, graunt$lx.17th[3:9], rev(halley$px[12:85]))
7. 차이를 비교하기 쉽도록 빗금으로 표시하되, 윗부분 먼저 표시. 
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)
abline(v=c(0, 76, 85), lty=2)
text(x = c(16, 36), y=c(20, 50), label=c("Graunt", "Halley"))
title(main="Graunt와 Halley의 생존 곡선 비교", xlab="연령(세)", ylab="생존률(%)")
polygon(poly.1.x, poly.1.y, angle=45, density=15, col="blue")

8. 아랫부분 표시. 
plot(px ~ age, data = halley, ann = F, xaxt = "n", yaxt = "n", type="l")
points(px[age %in% graunt$x]~age[age %in% graunt$x], data=halley)
lines(lx.17th~x, data=graunt, type="b")
axis(side=1, at=c(graunt$x, 85), labels=c(graunt$x, 85))
axis(side=2, at=graunt$lx.17th, labels=graunt$lx.17th, las=1)
abline(v=c(0, 76, 85), lty=2)
text(x = c(16, 36), y=c(20, 50), label=c("Graunt", "Halley"))
title(main="Graunt와 Halley의 생존 곡선 비교", xlab="연령(세)", ylab="생존률(%)")
polygon(poly.1.x, poly.1.y, angle=45, density=15, col="blue")
polygon(poly.2.x, poly.2.y, angle=45, density=15, col="red")

(ex.halley <- area.R(halley$age, halley$px)/100)
## [1] 27.872
(ex.graunt <- area.R(graunt$x, graunt$lx.17th)/100)
## [1] 18.17

ggplot에 한글 정보를 입력하려면,

library(ggplot2)

생존함수 비교

  • ggplot의 구조로부터 마지막 도표만 다시 나타내면,
g3

  • 그림의 전체 제목과 x-축, y-축, 범례를 한글로 바꿔 주기 위하여 extrafont 패키지를 설치하고 시스템 폰트를 등록한 후 theme()을 한글로 설정하여 저장.
theme.kr <- theme(axis.title.x = element_text(family = "HCR Dotum LVT"),
                  axis.title.y = element_text(family = "HCR Dotum LVT"),
                  axis.text.x = element_text(family = "HCR Dotum LVT"),
                  axis.text.y = element_text(family = "HCR Dotum LVT"),
                  plot.title = element_text(family = "HCR Dotum LVT"),
                  legend.title = element_text(family = "HCR Dotum LVT"),
                  legend.text = element_text(family = "HCR Dotum LVT"))
1. 지난 시간에 그린 `g3`에 theme.kr을 적용하고, x-축과 y-축의 라벨 수정. 
(g4 <- g3 + theme.kr +
   xlab("연령") + ylab("생존률(%)"))

2. 전체 제목 추가
(g4 <- g3 + theme.kr +
   xlab("연령") + ylab("생존률(%)") +
   ggtitle("Graunt 시대와 1993년 미국의 생존함수 비교"))

3. 범례 제목 한글로 수정.
(g4 <- g3 + theme.kr +
   xlab("연령") + ylab("생존률(%)") +
   ggtitle("Graunt 시대와 1993년 미국의 생존함수 비교") +
   labs(colour="시기"))

4. 범례의 항목 한글로 수정.
(g4 <- g3 + theme.kr +
   xlab("연령") + ylab("생존률(%)") +
   ggtitle("Graunt 시대와 1993년 미국의 생존함수 비교") +
   labs(colour="시기") +
   scale_colour_discrete(labels = c("Graunt 시대", "1993년 미국")))

  • 범례를 그림 안쪽에 위치시키려면,
(g5 <- g4 + theme(legend.position = c(0.8, 0.5)))

  • x-축의 눈금과 y-축의 눈금을 낯익은 값들로 조정.
(g6 <- g5 + scale_x_continuous(breaks=graunt$x) + scale_y_continuous(breaks=graunt$lx.17th)
   )

기대수명 비교

  • polygon 으로 그린 최종 작품에 한글 정보 입력.
    1. p4에서 출발.
p4

2. 앞과 같은 방법으로 타이틀과 범례에 한글 입력.
(p5 <- p4 + theme.kr +
   xlab("연령") + ylab("생존률(%)") +
   ggtitle("Graunt 시대와 1993년 미국의 기대수명 차이"))

3. "Graunt 시대", "1993년 미국", "기대수명의 차이"를 각각 필요한 위치 삽입.
(p6 <- p5 + annotate("text", x = c(20, 40, 70), y = c(20, 60, 90), label=c("Graunt 시대", "기대수명의 차이", "1993년 미국"), family="HCR Dotum LVT"))

4. x-축의 눈금과 y-축의 눈금을 낯익은 값들로 조정.
(p7 <- p6 + scale_x_continuous(breaks=graunt$x) + scale_y_continuous(breaks=graunt$lx.17th))

  • theme.kr은 어떻게 생겼는가?
theme.kr
## List of 7
##  $ axis.title.x:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.y:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.y :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.title  :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.title:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.text :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  - attr(*, "class")= chr [1:2] "theme" "gg"
##  - attr(*, "complete")= logi FALSE
str(theme.kr)
## List of 7
##  $ axis.title.x:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.y:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.y :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.title  :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.title:List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.text :List of 8
##   ..$ family    : chr "HCR Dotum LVT"
##   ..$ face      : NULL
##   ..$ colour    : NULL
##   ..$ size      : NULL
##   ..$ hjust     : NULL
##   ..$ vjust     : NULL
##   ..$ angle     : NULL
##   ..$ lineheight: NULL
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  - attr(*, "class")= chr [1:2] "theme" "gg"
##  - attr(*, "complete")= logi FALSE
class(theme.kr)
## [1] "theme" "gg"
  • 이 세션에서 만든 함수를 어떻게 저장하는 지 유의. 불러올 떄는 source() 사용.
dump("area.R", file="area.R")
save.image("graunt_0915.rda")