타이타닉호의 침몰 과정에서 여성과 어린이를 먼저 구한다는 원칙은 지켜졌는가?

Data

library(magrittr)
library(knitr)
library(pander)
library(printr)
library(ggplot2)
library(grid)
library(gridExtra)
load("Titanic_1801.RData")
ls()
##  [1] "Adult_df"     "b1"           "b1_p"         "b2"          
##  [5] "b2_p"         "b3"           "b3_p"         "b4"          
##  [9] "b4_p"         "b5"           "b5_p"         "Child_Class" 
## [13] "Child_df"     "Female_Class" "Female_df"    "Male_df"     
## [17] "p1"           "p1_text"      "p2"           "p2_text"     
## [21] "p3"           "p3_text"      "p4"           "p4_text"     
## [25] "p5"           "p5_text"      "Surv_Age"     "Surv_Class"  
## [29] "Surv_Sex"     "Titanic"      "y1_text"      "y2_text"     
## [33] "y3_text"      "y4_text"      "y5_text"

Source User Defined Functions

source("./barplot_gg.R")
source("./mosaic_gg.R")
source("./theme_kr.R")

Data Manipulation

Surv_Class %>% str
##  num [1:2, 1:4] 122 203 167 118 528 178 673 212
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Class   : chr [1:4] "1st" "2nd" "3rd" "Crew"
Surv_Class %>% as.table %>% str
##  'table' num [1:2, 1:4] 122 203 167 118 528 178 673 212
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Class   : chr [1:4] "1st" "2nd" "3rd" "Crew"
Surv_Sex %>% str
##  num [1:2, 1:2] 1364 367 126 344
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Sex     : chr [1:2] "Male" "Female"
Surv_Sex %>% as.table %>% str
##  'table' num [1:2, 1:2] 1364 367 126 344
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Sex     : chr [1:2] "Male" "Female"
Surv_Age %>% str
##  num [1:2, 1:2] 52 57 1438 654
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Age     : chr [1:2] "Child" "Adult"
Surv_Age %>% as.table %>% str
##  'table' num [1:2, 1:2] 52 57 1438 654
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Age     : chr [1:2] "Child" "Adult"
Female_Class %>% str
##  'xtabs' num [1:2, 1:4] 4 141 13 93 106 90 3 20
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Class   : chr [1:4] "1st" "2nd" "3rd" "Crew"
##  - attr(*, "call")= language xtabs(formula = Freq ~ Survived + Class, data = ., drop.unused.levels = TRUE)
Child_Class %>% str
##  'xtabs' num [1:2, 1:4] 0 6 0 24 52 27 0 0
##  - attr(*, "dimnames")=List of 2
##   ..$ Survived: chr [1:2] "No" "Yes"
##   ..$ Class   : chr [1:4] "1st" "2nd" "3rd" "Crew"
##  - attr(*, "call")= language xtabs(formula = Freq ~ Survived + Class, data = ., drop.unused.levels = TRUE)

Plots

By Class

Barplots

b1_stack <- barplot_gg_stack(as.data.frame(as.table(Surv_Class)[2:1, ]), 
#                              ggtitle = "객실 등급별 생존/사망",
                             xlab = "객실 등급", 
                             fill_name = "생존?") + 
  theme_kr + 
  guides(fill = FALSE) +
  theme(plot.title = element_text(hjust = 0.5))
b1_fill <- barplot_gg_fill(as.data.frame(as.table(Surv_Class)[2:1, ]), 
#                            ggtitle = "객실 등급별 생존/사망",
                           xlab = "객실 등급", 
                           fill_name = "") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))
top1 <-textGrob("객실 등급별 생존/사망", gp = gpar(cex = 1.2, fontfamily = "HCR Dotum LVT"))
grid.arrange(b1_stack, b1_fill, ncol = 2, top = top1)

Mosaic Plots

mosaic_gg(as.table(Surv_Class)[2:1, ], 
          ggtitle = "객실 등급별 생존/사망",
          xlab = "객실 등급", 
          fill_name = "생존?") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))

ggsave("../pics/Titanic_mosaic_ggplot01.png", width = 8, height = 6, dpi = 72)

By Sex

Barplots

b2_stack <- barplot_gg_stack(as.data.frame(as.table(Surv_Sex)[2:1, ]), 
#                              ggtitle = "객실 등급별 생존/사망",
                             xlab = "남녀", 
                             fill_name = "생존?") + 
  theme_kr + 
  guides(fill = FALSE) +
  theme(plot.title = element_text(hjust = 0.5))
b2_fill <- barplot_gg_fill(as.data.frame(as.table(Surv_Sex)[2:1, ]), 
#                            ggtitle = "객실 등급별 생존/사망",
                           xlab = "남녀", 
                           fill_name = "") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))
top2 <-textGrob("남녀별 생존/사망", gp = gpar(cex = 1.2, fontfamily = "HCR Dotum LVT"))
grid.arrange(b2_stack, b2_fill, ncol = 2, top = top2)

Mosaic Plots

mosaic_gg(as.table(Surv_Sex)[2:1, ], 
          ggtitle = "남녀별 생존/사망", 
          xlab = "객실 등급", 
          fill_name = "생존?") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))

ggsave("../pics/Titanic_mosaic_ggplot02.png", width = 8, height = 6, dpi = 72)

By Age

Barplots

b3_stack <- barplot_gg_stack(as.data.frame(as.table(Surv_Age)[2:1, ]), 
#                              ggtitle = "객실 등급별 생존/사망",
                             xlab = "어린이와 어른", 
                             fill_name = "생존?") + 
  theme_kr + 
  guides(fill = FALSE) +
  theme(plot.title = element_text(hjust = 0.5))
b3_fill <- barplot_gg_fill(as.data.frame(as.table(Surv_Age)[2:1, ]), 
#                            ggtitle = "객실 등급별 생존/사망",
                           xlab = "어린이와 어른", 
                           fill_name = "") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))
top3 <-textGrob("어린이와 어른의 생존/사망", gp = gpar(cex = 1.2, fontfamily = "HCR Dotum LVT"))
grid.arrange(b3_stack, b3_fill, ncol = 2, top = top3)

Mosaic Plots

mosaic_gg(as.table(Surv_Age)[2:1, ], 
          ggtitle = "어린이/어른 생존/사망", 
          xlab = "객실 등급", 
          fill_name = "생존?") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))

ggsave("../pics/Titanic_mosaic_ggplot03.png", width = 8, height = 6, dpi = 72)

Female by Class

Barplots

b4_stack <- barplot_gg_stack(as.data.frame(as.table(Female_Class)[2:1, ]), 
#                              ggtitle = "객실 등급별 생존/사망",
                             xlab = "객실 등급", 
                             fill_name = "생존?") + 
  theme_kr + 
  guides(fill = FALSE) +
  theme(plot.title = element_text(hjust = 0.5))
b4_fill <- barplot_gg_fill(as.data.frame(as.table(Female_Class)[2:1, ]), 
#                            ggtitle = "객실 등급별 생존/사망",
                           xlab = "객실 등급", 
                           fill_name = "") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))
top4 <-textGrob("객실 등급별 여성 생존/사망", gp = gpar(cex = 1.2, fontfamily = "HCR Dotum LVT"))
grid.arrange(b4_stack, b4_fill, ncol = 2, top = top4)

Mosaic Plots

mosaic_gg(as.table(Female_Class)[2:1, ], 
          ggtitle = "객실 등급별 여성 생존/사망", 
          xlab = "객실 등급", 
          fill_name = "생존?") + 
  theme_kr +  
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))

ggsave("../pics/Titanic_mosaic_ggplot04.png", width = 8, height = 6, dpi = 72)

Children by Class

Barplots

b5_stack <- barplot_gg_stack(as.data.frame(as.table(Child_Class)[2:1, ]), 
#                              ggtitle = "객실 등급별 생존/사망",
                             xlab = "객실 등급", 
                             fill_name = "생존?") + 
  theme_kr + 
  guides(fill = FALSE) +
  theme(plot.title = element_text(hjust = 0.5))
b5_fill <- barplot_gg_fill(as.data.frame(as.table(Child_Class)[2:1, ]), 
#                            ggtitle = "객실 등급별 생존/사망",
                           xlab = "객실 등급", 
                           fill_name = "") + 
  theme_kr + 
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5))
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.
top5 <-textGrob("객실 등급별 어린이 생존/사망", gp = gpar(cex = 1.2, fontfamily = "HCR Dotum LVT"))
grid.arrange(b5_stack, b5_fill, ncol = 2, top = top4)

Mosaic Plots

mosaic_gg(as.table(Child_Class)[2:1, -4], 
          ggtitle = "객실 등급별 어린이 생존/사망", 
          xlab = "객실 등급", 
          fill_name = "생존?") + 
  theme_kr +  
  scale_fill_manual(name = "", labels = c("생존", "사망"), values = rainbow(2)[2:1]) +
  theme(plot.title = element_text(hjust = 0.5)) 
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.

ggsave("../pics/Titanic_mosaic_ggplot05.png", width = 8, height = 6, dpi = 72)