Data

원자료는 세종실록, 요약표는 오기수 교수의 논문에서.

지역 계급 관료여부 찬반 집계
서울 대신 등 관료 찬성 21
서울 대신 등 관료 반대 194
서울 3품이하현직 관료 찬성 259
서울 3품이하현직 관료 반대 393
서울 3품이하전직 관료 찬성 443
서울 3품이하전직 관료 반대 117
유후사 품관촌민 품관촌민 찬성 1123
유후사 품관촌민 품관촌민 반대 71
경기 수령 관료 찬성 29
경기 수령 관료 반대 5
경기 품관촌민 품관촌민 찬성 17076
경기 품관촌민 품관촌민 반대 236
평안 대신 등 관료 반대 1
평안 수령 관료 찬성 6
평안 수령 관료 반대 35
평안 품관촌민 품관촌민 찬성 1326
평안 품관촌민 품관촌민 반대 28474
황해 수령 관료 찬성 17
황해 수령 관료 반대 17
황해 품관촌민 품관촌민 찬성 4454
황해 품관촌민 품관촌민 반대 15601
충청 대신 등 관료 반대 2
충청 수령 관료 찬성 35
충청 수령 관료 반대 26
충청 품관촌민 품관촌민 찬성 6982
충청 품관촌민 품관촌민 반대 14013
강원 수령 관료 찬성 5
강원 수령 관료 반대 10
강원 품관촌민 품관촌민 찬성 939
강원 품관촌민 품관촌민 반대 6888
함길 대신 등 관료 반대 1
함길 수령 관료 찬성 3
함길 수령 관료 반대 14
함길 품관촌민 품관촌민 찬성 75
함길 품관촌민 품관촌민 반대 7387
경상 수령 관료 찬성 55
경상 수령 관료 반대 16
경상 품관촌민 품관촌민 찬성 36262
경상 품관촌민 품관촌민 반대 377
전라 대신 등 관료 반대 2
전라 수령 관료 찬성 42
전라 수령 관료 반대 12
전라 품관촌민 품관촌민 찬성 29505
전라 품관촌민 품관촌민 반대 257

전체 찬반

# pander(format(tbl.vote, big.mark = ","))
kable(t(as.matrix(format(tbl.vote, big.mark = ","))), caption = "전체 찬반", align = 'c')
전체 찬반
찬성 반대
98,657 74,149
kable(t(as.table(format(tbl.vote, big.mark = ","))), caption = "전체 찬반", align = 'c')
전체 찬반
찬성 반대
98,657 74,149
kable(t(as.table(format(prop.table(tbl.vote)*100, digits = 3, nsmall = 1))), caption = "전체 찬반(%)", align = 'c')
전체 찬반(%)
찬성 반대
57.1 42.9

원형그래프

R-Base Plot

par(family = "Malgun Gothic")
gray.2 <- gray.colors(12)[c(12, 7)]
rainbow.2 <- rainbow(2)[2:1]
pie(tbl.vote, col = rainbow.2)
title(main = "전체 찬반")
text(x = 0, y = c(0.4, -0.4), labels = format(tbl.vote, big.mark = ","))

# dev.copy(png, "../pics/sejong_total_pie.png", width = 480, height = 480)
# dev.off()

ggplot2

source("./theme_Malgun_kr.R")
str(theme.kr)
## List of 7
##  $ axis.title.x:List of 8
##   ..$ family    : chr "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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 "Malgun Gothic"
##   ..$ 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
par(family = "Malgun Gothic")
vote.df <- as.data.frame(tbl.vote)
# y.coord <- cumsum(vote.df$Freq)/2 + c(0, cumsum(head(vote.df$Freq, -1))/2)
y.coord <- cumsum(vote.df$Freq)/2
pie.label <- paste(levels(vote.df$vote), format(vote.df$Freq, big.mark = ","), sep = "\n") 
str(vote.df)
## 'data.frame':    2 obs. of  2 variables:
##  $ vote: Factor w/ 2 levels "찬성","반대": 1 2
##  $ Freq: int  98657 74149
p1 <- ggplot(vote.df, aes(x = "", y = Freq, fill = vote)) 
p2 <- p1 + 
  geom_bar(width = 1, stat = "identity", position = position_stack(reverse = TRUE))
p2

p2.2 <- p1 + 
  geom_bar(width = 1, stat = "identity", position = "dodge")
p2.2

p3.2 <- p2.2 + 
  theme_bw()
p3.2

p4.2 <- p3.2 + 
  theme.kr
p4.2 

p5.2 <- p4.2 + 
  scale_x_discrete(name = "찬반") +
  scale_y_continuous(name = "집계", breaks = vote.df$Freq, labels = format(vote.df$Freq, big.mark = ","))
p5.2

p6.2 <- p5.2 +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], labels = vote.df$vote)
p6.2

p7.2 <- p6.2 +
  theme(legend.position = c(0.8, 0.85))
p7.2

# ggsave("../pics/sejong_geom_bar_total_ggplot.png", p7.2, dpi = 72)
p3 <- p2 + 
  theme_bw()
p3

p4 <- p3 + 
  theme.kr
p4 

p5 <- p4 + 
  scale_x_discrete(name = "찬반") +
  scale_y_continuous(name = "집계", breaks = cumsum(vote.df$Freq), labels = format(cumsum(vote.df$Freq), big.mark = ","))
p5

p6 <- p5 +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], labels = vote.df$vote)
p6

# ggsave("../pics/sejong_geom_bar_total_ggplot_stack.png", p6, dpi = 72)
p7 <- p6 +
  theme(legend.position = c(0.5, 0.25))
p7

pie.total.1 <- p2 + 
  coord_polar(theta = "y", start = 3*pi/2, direction = -1)
pie.total.1

pie.total.2 <- pie.total.1 + 
  scale_y_continuous(name = "", breaks = NULL) +
  scale_x_discrete(name = "") 
pie.total.2

pie.total.3 <- pie.total.2 +
  scale_fill_manual(name = "", values = rainbow(2)[2:1])
pie.total.3

pie.total.4 <- pie.total.3 +
  theme_void(base_family = "Malgun Gothic")
pie.total.4

pie.total.5 <- pie.total.4 +
  guides(fill = "none")
pie.total.5

pie.total.6 <- pie.total.5 +
#  geom_text(aes(y = y.coord), label = pie.label, family = "Malgun Gothic")
  geom_text(aes(y = y.coord), label = pie.label, family = "Malgun Gothic", position = position_stack(reverse = TRUE))
pie.total.6

pie.total.7 <- pie.total.6 +
  ggtitle("전체 찬반") +
  theme(plot.margin = unit(c(1, 1, 1.5, 1), "lines"))
pie.total.7

# ggsave("../pics/sejong_total_pie_ggplot.png", pie.total.7, dpi = 72)

pie.gg()

pie.gg <- function(df, ggtitle = "", font.family = ""){
  n <- length(names(df))
  y.coord <- cumsum(df$Freq)
  pie.label <- paste(levels(df$vote), format(df$Freq, big.mark = ","), sep = "\n") 
  p1 <- ggplot(df, aes(x = "", y = Freq, fill = vote)) 
  p2 <- p1 + 
    geom_bar(width = 1, stat = "identity", position = position_stack(reverse = TRUE))
  pie.1 <- p2 + 
    coord_polar(theta = "y", start = 3*pi/2, direction = -1)
  pie.2 <- pie.1 + 
    scale_y_continuous(name = "", breaks = NULL) +
    scale_x_discrete(name = "") 
  pie.3 <- pie.2 +
    scale_fill_manual(name = "", values = rainbow(n)[n:1])
  pie.4 <- pie.3 +
    theme_void(base_family = font.family)
  pie.5 <- pie.4 +
    guides(fill = "none")
  pie.6 <- pie.5 +
    geom_text(aes(y = y.coord/n), label = pie.label, family = font.family, position = position_stack(reverse = TRUE))
  pie.7 <- pie.6 +
    ggtitle(ggtitle) + 
    theme(plot.margin = unit(c(1, 1, 1.5, 1), "lines"))
  return(pie.7)
}
dump("pie.gg", file = "./pie.gg.R")
pie.gg(vote.df, ggtitle = "전체 찬반", font.family = "Malgun Gothic")

계급 및 지역별 찬반

계급별 찬반

kable(format(vote.class, big.mark = ","), align = "r", caption = "계급별 찬반")
계급별 찬반
대신 등 3품이하현직 3품이하전직 수령 품관촌민
찬성 21 259 443 192 97,742
반대 200 393 117 135 73,304
kable(format(prop.table(vote.class, margin = 2)*100, digits = 3, nsmall = 1), align = "r", caption = "계급별 찬반(%)")
계급별 찬반(%)
대신 등 3품이하현직 3품이하전직 수령 품관촌민
찬성 9.5 39.7 79.1 58.7 57.1
반대 90.5 60.3 20.9 41.3 42.9

품관촌민 별도 분석

품관촌민의 수효가 상대적으로 많아서

kable(format(vote.class.2, big.mark = ","), align = rep("r", 2), caption = "품관촌민")
품관촌민
관료 품관촌민
찬성 915 97,742
반대 845 73,304

소계를 교차표의 주변에 계산

kable(format(vote.class.2.am, big.mark = ","), caption = "관료와 품관촌민(소계)", align = rep("r", 3))
관료와 품관촌민(소계)
관료 품관촌민
찬성 915 97,742 98,657
반대 845 73,304 74,149
1,760 171,046 172,806

백분율을 계산하여 주변에 집계.

kable(format(prop.table(vote.class.2, margin = 2)*100, digits = 3, nsmall = 1), caption = "관료와 품관촌민(%)", align = rep("r", 3))
관료와 품관촌민(%)
관료 품관촌민
찬성 52.0 57.1
반대 48.0 42.9

원형그래프

R-Base Plot

par(family = "Malgun Gothic")
par(mfrow = c(1, 2))
pie(vote.class.2[, 1], labels = c("찬성", "반대"), col = rainbow.2)
title(main = "관료의 찬반")
text(x = 0, y = c(0.4, -0.4), labels = vote.class.2[, 1])
pie(vote.class.2[, 2], labels = c("찬성", "반대"), col = rainbow.2)
title(main = "품관촌민의 찬반")
text(x = 0, y = c(0.4, -0.4), labels = format(vote.class.2[, 2], big.mark = ","))

# dev.copy(png, "../pics/sejong_bureaus_commons_pie.png", width = 960, height = 480)
# dev.off()
par(mfrow = c(1, 1))

ggplot2

vote.class.2.df <- as.data.frame(vote.class.2)
v.names.class.kr <- c("찬반", "계급", "집계")
kable(format(vote.class.2.df, big.mark = ","), align = c('c', 'c', 'r'), col.names = v.names.class.kr, caption = "관료와 품관촌민")
관료와 품관촌민
찬반 계급 집계
찬성 관료 915
반대 관료 845
찬성 품관촌민 97,742
반대 품관촌민 73,304
vote.bureaus.df <- subset(vote.class.2.df, vote.class.2.df$class.2 == "관료", select = c("vote", "Freq"))
kable(format(vote.bureaus.df, big.mark = ","), align = 'r', col.names = v.names.class.kr[-2], caption = "관료의 찬반")
관료의 찬반
찬반 집계
찬성 915
반대 845
vote.commons.df <- subset(vote.class.2.df, vote.class.2.df$class.2 == "품관촌민", select = c("vote", "Freq"))
kable(format(vote.commons.df, big.mark = ","), align = 'r', row.names = FALSE, col.names = v.names.class.kr[-2], caption = "품관촌민의 찬반")
품관촌민의 찬반
찬반 집계
찬성 97,742
반대 73,304
pie.bureaus <- pie.gg(vote.bureaus.df, ggtitle = "관료의 찬반", font.family = "Malgun Gothic")
pie.commons <- pie.gg(vote.commons.df, ggtitle = "품관촌민의 찬반", font.family = "Malgun Gothic")
pies.grid <- grid.arrange(pie.bureaus, pie.commons, ncol = 2, top = "")

pies.grid
## TableGrob (2 x 2) "arrange": 3 grobs
##   z     cells    name                 grob
## 1 1 (2-2,1-1) arrange       gtable[layout]
## 2 2 (2-2,2-2) arrange       gtable[layout]
## 3 3 (1-1,1-2) arrange text[GRID.text.1139]
# ggsave("../pics/sejong_bureaus_commons_pie_ggplot.png", pies.grid, width = 8, height = 4, dpi = 72)

지역별 찬반

관료와 품관촌민

kable(vote.region.bureaus, caption = "관료들의 지역별 찬반")
관료들의 지역별 찬반
서울 경기 평안 황해 충청 강원 함길 경상 전라
찬성 723 29 6 17 35 5 3 55 42
반대 704 5 36 17 28 10 15 16 14
kable(format(prop.table(vote.region.bureaus, margin = 2)*100, digits = 3, nsmall = 1), align = rep("r", 9), caption = "관료들의 지역별 찬반(%)")
관료들의 지역별 찬반(%)
서울 경기 평안 황해 충청 강원 함길 경상 전라
찬성 50.7 85.3 14.3 50.0 55.6 33.3 16.7 77.5 75.0
반대 49.3 14.7 85.7 50.0 44.4 66.7 83.3 22.5 25.0
kable(format(vote.region.commons, big.mark = ","), align = rep("r", 9), caption = "품관촌민들의 지역별 찬반")
품관촌민들의 지역별 찬반
유후사 경기 평안 황해 충청 강원 함길 경상 전라
찬성 1,123 17,076 1,326 4,454 6,982 939 75 36,262 29,505
반대 71 236 28,474 15,601 14,013 6,888 7,387 377 257
kable(format(prop.table(vote.region.commons, margin = 2)*100, digits = 1, nsmall = 1), align = rep("r", 9), caption = "품관촌민들의 지역별 찬반(%)")
품관촌민들의 지역별 찬반(%)
유후사 경기 평안 황해 충청 강원 함길 경상 전라
찬성 94.1 98.6 4.4 22.2 33.3 12.0 1.0 99.0 99.1
반대 5.9 1.4 95.6 77.8 66.7 88.0 99.0 1.0 0.9

서울의 찬반

kable(vote.seoul.class, caption = "서울의 찬반")
서울의 찬반
대신 등 3품이하현직 3품이하전직
찬성 21 259 443
반대 194 393 117
kable(format(prop.table(vote.seoul.class, margin = 2)*100, digits = 1, nsmall = 1), align = rep("r", 3), caption = "서울의 찬반(%)")
서울의 찬반(%)
대신 등 3품이하현직 3품이하전직
찬성 9.8 39.7 79.1
반대 90.2 60.3 20.9

막대그래프

R-Base Plot

서울의 계급별 찬반을 barplot으로 그려봄. text()의 좌표는 locator()로 찾아냄.

par(family = "Malgun Gothic")
barplot(vote.seoul.class, col = rainbow.2)
title(main = "서울의 찬반")
text(x = c(0.7, 1.9, 1.9, 3.1, 3.1), y = c(120, 450, 135, 500, 220), labels = c("194","393", "259", "117", "443"))
legend("topleft", inset = 0.05, fill = rainbow.2, legend = c("찬성", "반대"))

# dev.copy(png, "../pics/sejong_seoul_barplot.png", width = 480, height = 360)
# dev.off()

ggplot

Stack
vote.seoul.df <- as.data.frame(vote.seoul.class)
x.stack <- vote.seoul.df[, 2]
# y.stack <- unlist(tapply(vote.seoul.df$Freq, vote.seoul.df$class, function(x){cumsum(x) + c(0, cumsum(head(x, -1)))}))
y.stack <- unlist(tapply(vote.seoul.df$Freq, vote.seoul.df$class, cumsum))
b1.seoul <- ggplot(vote.seoul.df, aes(x = x.stack, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = position_stack())
b1.seoul

b2.seoul <- b1.seoul +
  theme_bw(base_family = "Malgun Gothic") +
  theme.kr +
  scale_x_discrete(name = "계급") +
  scale_y_continuous(name = "집계", breaks = vote.seoul.df$Freq, labels = vote.seoul.df$Freq) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1])
b2.seoul  

b3.seoul <- b2.seoul +
# geom_text(aes(y = y.stack/2), label = vote.seoul.df$Freq)
  geom_text(aes(y = y.stack/2), label = vote.seoul.df$Freq, position = position_stack())
b3.seoul

# ggsave("../pics/sejong_seoul_barplot_stack_ggplot.png", b3.seoul, width = 6, height = 4.5, dpi = 72)

Dodge

b1.seoul.dodge <- ggplot(vote.seoul.df, aes(x = x.stack, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = "dodge")
b1.seoul.dodge

b2.seoul.dodge <- b1.seoul.dodge +
  theme_bw(base_family = "Malgun Gothic") +
  theme.kr +
  scale_x_discrete(name = "계급") +
  scale_y_continuous(name = "집계", breaks = vote.seoul.df$Freq, labels = vote.seoul.df$Freq) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1])
b2.seoul.dodge  

b3.seoul.dodge <- b2.seoul.dodge +
  geom_text(aes(y = vote.seoul.df$Freq/2), label = vote.seoul.df$Freq, position = position_dodge(width = 0.9)) +
  ggtitle("서울의 찬반")
b3.seoul.dodge

# ggsave("../pics/sejong_seoul_barplot_dodge_ggplot.png", b3.seoul.dodge, width = 6, height = 4.5, dpi = 72)

Fill

y.fill <- unlist(tapply(vote.seoul.df$Freq, x.stack, function(x){cumsum(x)/sum(x)}))
b1.seoul.fill <- ggplot(vote.seoul.df, aes(x = x.stack, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = position_fill())
b1.seoul.fill

b2.seoul.fill <- b1.seoul.fill +
  theme_bw(base_family = "Malgun Gothic") +
  theme.kr +
  scale_x_discrete(name = "계급") +
  scale_y_continuous(name = "집계", breaks = vote.seoul.df$Freq, labels = vote.seoul.df$Freq) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1])
b2.seoul.fill  

b3.seoul.fill <- b2.seoul.fill +
  geom_text(aes(y = y.fill/2), label = vote.seoul.df$Freq, position = position_stack()) +
  ggtitle("서울의 찬반")
b3.seoul.fill

# ggsave("../pics/sejong_seoul_barplot_fill_ggplot.png", b3.seoul.fill, width = 6, height = 4.5, dpi = 72)

barplot.gg

barplot.gg.stack <- function(df, base_family = "", ggtitle = "", xlab = ""){
x <- df[, 2]
y <- unlist(tapply(df$Freq, x, cumsum))
b1 <- ggplot(df, aes(x = x, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = position_stack())
b2 <- b1 +
  theme_bw(base_family = base_family) +
#  theme.kr +
  scale_x_discrete(name = xlab) +
  scale_y_continuous(name = "집계", breaks = NULL) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], guide = guide_legend())
b3 <- b2 +
  geom_text(aes(y = y/2), label = format(df$Freq, big.mark = ","), position = position_stack()) +
  ggtitle(ggtitle)
return(b3)
}
barplot.gg.dodge <- function(df, base_family = "", ggtitle = "", xlab = ""){
x <- df[, 2]
y <- unlist(tapply(df$Freq, x, cumsum))
b1 <- ggplot(df, aes(x = x, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = "dodge")
b2 <- b1 +
  theme_bw(base_family = base_family) +
#  theme.kr +
  scale_x_discrete(name = xlab) +
  scale_y_continuous(name = "집계", breaks = NULL) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1])
b3 <- b2 +
  geom_text(aes(y = df$Freq/2), label = format(df$Freq, big.mark = ","), position = position_dodge(width = 0.9)) +
  ggtitle(ggtitle)
return(b3)
}
barplot.gg.fill <- function(df, base_family = "", ggtitle = "", xlab = ""){
x <- df[, 2]
y <- unlist(tapply(df$Freq, x, function(x){cumsum(x)/sum(x)}))
b1 <- ggplot(df, aes(x = x, y = Freq, fill = rev(vote))) +
  geom_bar(stat = "identity", position = position_fill())
b2 <- b1 +
  theme_bw(base_family = base_family) +
#  theme.kr +
  scale_x_discrete(name = xlab) +
  scale_y_continuous(name = "집계", breaks = NULL) +
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], guide = guide_legend())
b3 <- b2 +
  geom_text(aes(y = y/2), label = format(df$Freq, big.mark = ","), position = position_stack()) +
  ggtitle(ggtitle)
return(b3)
}
barplot.gg <- function(x, position, base_family = "", ggtitle = "", xlab = ""){
  switch(position,
         stack = barplot.gg.stack(x, base_family = base_family, ggtitle = ggtitle, xlab = xlab),
         dodge = barplot.gg.dodge(x, base_family = base_family, ggtitle = ggtitle, xlab = xlab),
         fill = barplot.gg.fill(x, base_family = base_family, ggtitle = ggtitle, xlab = xlab))
}
(bar_seoul_stack <- barplot.gg(vote.seoul.df, position = "stack", base_family = "Malgun Gothic", ggtitle = "서울의 찬반", xlab = "계급"))

# ggsave("../pics/sejong_seoul_barplot_stack_ggplotv2.png", bar_seoul_stack, width = 6, height = 4.5, dpi = 72)
(bar_seoul_dodge <- barplot.gg(vote.seoul.df, position = "dodge", base_family = "Malgun Gothic", ggtitle = "서울의 찬반", xlab = "계급"))

# ggsave("../pics/sejong_seoul_barplot_dodge_ggplotv2.png", bar_seoul_dodge, width = 6, height = 4.5, dpi = 72)
(bar_seoul_fill <- barplot.gg(vote.seoul.df, position = "fill", base_family = "Malgun Gothic", ggtitle = "서울의 찬반", xlab = "계급"))

# ggsave("../pics/sejong_seoul_barplot_fill_ggplotv2.png", bar_seoul_fill, width = 6, height = 4.5, dpi = 72)
dump(c("barplot.gg","barplot.gg.stack", "barplot.gg.dodge", "barplot.gg.fill"), file = "./barplot.gg.R")

mosaic plot

R-Base Plot

par(family = "Malgun Gothic")
mosaicplot(t(vote.seoul.class), col = rainbow.2, main = "서울의 찬반", xlab = "계급" , ylab = "찬반")

# dev.copy(png, "../pics/sejong_seoul_mosaic.png", width = 480, height = 360)
# dev.off()

ggplot

vote.seoul.class.df <- as.data.frame(vote.seoul.class)
vote.seoul.class.sum <- tapply(vote.seoul.class.df$Freq, vote.seoul.class.df[, 2], sum)
vote.seoul.class.p.m <- prop.table(vote.seoul.class.sum)
vote.seoul.class.p <- prop.table(vote.seoul.class)
vote.seoul.class.p.2 <- prop.table(vote.seoul.class, margin = 2)
vote.seoul.class.p.df <- as.data.frame(vote.seoul.class.p)
vote.seoul.class.p.df$width <- vote.seoul.class.p.m[match(vote.seoul.class.p.df$class, names(vote.seoul.class.p.m))]
vote.seoul.class.p.df$height <- as.data.frame(vote.seoul.class.p.2)$Freq
vote.seoul.class.p.df$label.height <- unlist(tapply(vote.seoul.class.p.df$height, vote.seoul.class.p.df$class, cumsum))
x.center <- (cumsum(vote.seoul.class.p.m) + c(0, head(cumsum(vote.seoul.class.p.m), -1)))/2
vote.seoul.class.p.df$center <- x.center[match(vote.seoul.class.p.df$class, names(x.center))]
m1 <- ggplot(vote.seoul.class.p.df, aes(x = center, y = height, width = width)) + 
  geom_bar(aes(fill = rev(vote)), stat = "identity", col = "white", size = 2, position = position_stack())
m1

m2 <- m1 + 
  theme_bw(base_family = "Malgun Gothic")
m2

m3 <- m2 + 
  geom_text(aes(x = center, y = 1.05), label = vote.seoul.class.p.df$class, family = "Malgun Gothic")
m3

m4 <- m3 + 
  geom_text(aes(x = center, y = label.height/2), label = format(vote.seoul.class.df$Freq, big.mark = ","), position = position_stack())
m4

x.breaks <- c(0, ifelse(cumsum(vote.seoul.class.p.m) < 0.1, 0.0, cumsum(vote.seoul.class.p.m)))
x.label <- format(x.breaks, digits = 2, nsmall = 2)
m5 <- m4 + 
  scale_x_continuous(name = "계급", breaks = x.breaks, label = x.label) + 
  scale_y_continuous(name = "찬반") + 
  scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], guide = guide_legend()) +
  ggtitle("서울의 찬반")
m5

source("./mosaic.gg.R")
mosaic_gg
## function (tbl, base_family = "", ggtitle = "", xlab = "") 
## {
##     tbl.df <- as.data.frame(tbl)
##     tbl.sum <- tapply(tbl.df$Freq, tbl.df[, 2], sum)
##     tbl.p.m <- prop.table(tbl.sum)
##     tbl.p <- prop.table(tbl)
##     tbl.p.2 <- prop.table(tbl, margin = 2)
##     tbl.p.df <- as.data.frame(tbl.p)
##     tbl.p.df$width <- tbl.p.m[match(tbl.p.df[, 2], names(tbl.p.m))]
##     tbl.p.df$height <- as.data.frame(tbl.p.2)$Freq
##     tbl.p.df$label.height <- unlist(tapply(tbl.p.df$height, tbl.p.df[, 
##         2], cumsum))
##     x.center <- (cumsum(tbl.p.m) + c(0, head(cumsum(tbl.p.m), 
##         -1)))/2
##     tbl.p.df$center <- x.center[match(tbl.p.df[, 2], names(x.center))]
##     m1 <- ggplot(tbl.p.df, aes(x = center, y = height, width = width)) + 
##         geom_bar(aes(fill = rev(vote)), stat = "identity", col = "white", 
##             size = 1, position = position_stack())
##     m1
##     m2 <- m1 + theme_bw(base_family = base_family)
##     m2
##     m3 <- m2 + geom_text(aes(x = center, y = 1.05), label = tbl.p.df[, 
##         2], family = base_family)
##     m3
##     m4 <- m3 + geom_text(aes(x = center, y = label.height/2), 
##         label = format(tbl.df$Freq, big.mark = ","), position = position_stack())
##     m4
##     x.breaks <- c(0, ifelse(cumsum(tbl.p.m) < 0.1, 0, cumsum(tbl.p.m)))
##     x.label <- format(x.breaks, digits = 2, nsmall = 2)
##     m5 <- m4 + scale_x_continuous(name = xlab, breaks = x.breaks, 
##         label = x.label) + scale_y_continuous(name = "찬반") + 
##         scale_fill_manual(name = "찬반", values = rainbow(2)[2:1], 
##             guide = guide_legend()) + ggtitle(ggtitle) + theme(plot.margin = unit(c(1, 
##         2, 1, 1), "lines"))
##     m5
##     return(m5)
## }
m5.seoul <- mosaic_gg(vote.seoul.class, base_family = "Malgun Gothic", ggtitle = "서울의 찬반", xlab = "계급")
m5.seoul

# ggsave("../pics/sejong_seoul_mosaic_ggplot.png", m5.seoul, width = 6, height = 4.5, dpi = 72)

지역별 관료들의 찬반(서울 제외)

kable(vote.region.bureaus[, -1], caption = "지역별 관료들의 찬반(서울 제외)")
지역별 관료들의 찬반(서울 제외)
경기 평안 황해 충청 강원 함길 경상 전라
찬성 29 6 17 35 5 3 55 42
반대 5 36 17 28 10 15 16 14
kable(format(prop.table(vote.region.bureaus[, -1], margin = 2)*100, digits = 1, nsmall = 1), align = "r", caption = "지역별 관료들의 찬반(서울 제외, %)")
지역별 관료들의 찬반(서울 제외, %)
경기 평안 황해 충청 강원 함길 경상 전라
찬성 85.3 14.3 50.0 55.6 33.3 16.7 77.5 75.0
반대 14.7 85.7 50.0 44.4 66.7 83.3 22.5 25.0

막대그래프

R-Base Plot

par(family = "Malgun Gothic")
barplot(vote.region.bureaus[, -1], beside = FALSE, col = rainbow.2)
title(main = "지방 관료들의 찬반")
legend("topleft", inset = 0.05, fill = rainbow.2, legend = c("찬성", "반대"))

# dev.copy(png, "../pics/sejong_bureaus.png", width = 720, height = 360)
# dev.off()

ggplot

vote.region.bureaus.df <- as.data.frame(vote.region.bureaus[, -1])
barplot.gg(vote.region.bureaus.df, position = "stack", base_family = "Malgun Gothic", ggtitle = "지방 관료들의 찬반", xlab = "지역")

# ggsave("../pics/sejong_bureaus_barplot_stack_ggplot.png", width = 9, height = 4.5, dpi = 72)

mosaic plot

R-Base Plot

par(family = "Malgun Gothic")
mosaicplot(t(vote.region.bureaus[, -1]), col = rainbow.2, main = "", xlab = "지역", ylab = "찬반")
title(main = "지방 관료들의 찬반")

# dev.copy(png, "../pics/sejong_bureaus_mosaic.png", width = 720, height = 360)
# dev.off()

ggplot

# source("./mosaic.gg.R")
m5.bureaus <- mosaic_gg(vote.region.bureaus[, -1], base_family = "Malgun Gothic", ggtitle = "지방관료의 찬반", xlab = "지역별 관료들의 비중")
m5.bureaus

# ggsave("../pics/sejong_bureaus_mosaic_ggplot.png", m5.bureaus, width = 9, height = 4.5, dpi = 72)

품관촌민들의 지역별 찬반

막대그래프

R-Base Plot

par(family = "Malgun Gothic")
barplot(vote.region.commons, beside = FALSE, col = rainbow.2, axes = FALSE)
axis(2, at = seq(0, 30000, by = 5000), labels = c("0", "", "1만", "", "2만", "", "3만"), las = 1)
title(main = "품관촌민들의 지역별 찬반")
legend("topleft", inset = 0.05, fill = rainbow.2, legend = c("찬성", "반대"))

# dev.copy(png, "../pics/sejong_commons_barplot.png", width = 720, height = 360)
# dev.off()

ggplot

vote.region.commons.df <- as.data.frame(vote.region.commons)
barplot.gg(vote.region.commons.df, position = "stack", base_family = "Malgun Gothic", ggtitle = "품관촌민의 찬반", xlab = "지역")

# ggsave("../pics/sejong_commons_barplot_stack_ggplot.png", width = 9, height = 4.5, dpi = 72)

mosaic plot

R-Base Plot

par(family = "Malgun Gothic")
mosaicplot(t(vote.region.commons), col = rainbow.2, main = "품관촌민의 지역별 찬반", xlab = "지역", ylab = "찬반")

# dev.copy(png, "../pics/sejong_commons_mosaic.png", width = 720, height = 360)
# dev.off()

ggplot

# source("./mosaic.gg.R")
m5.commons <- mosaic_gg(vote.region.commons, base_family = "Malgun Gothic", ggtitle = "품관촌민의 찬반", xlab = "지역별 품관촌민의 비중")
m5.commons

# ggsave("../pics/sejong_commons_mosaic_ggplot.png", m5.commons, width = 12, height = 4.5, dpi = 72)

충청도

관료들의 찬반과 품관촌민의 찬반이 다른 곳.

kable(format(vote.chung.class, big.mark = ","), caption = "충청도 사례", align = "r")
충청도 사례
대신 등 수령 품관촌민
찬성 0 35 6,982
반대 2 26 14,013
kable(format(prop.table(vote.chung.class, margin = 2)*100, digits = 3, nsmall = 1), caption = "충청도 사례", align = "r")
충청도 사례
대신 등 수령 품관촌민
찬성 0.0 57.4 33.3
반대 100.0 42.6 66.7

막대그래프

R-Base Plot

par(mar = c(5.1, 4.1, 4.1, 8.1), xpd = TRUE, family = "Malgun Gothic")
barplot(prop.table(vote.chung.class, margin = 2), col = rainbow.2, ylim = c(0,  1.1), axes = FALSE)
axis(side = 2, at = c(0, 0.5, 1.0), labels = c("0", "50%", "100%"), las = 1)
title(main = "충청도의 계급별 찬반 비율")
legend("right", inset = -0.3, fill = rainbow.2, legend = c("찬성", "반대"))
text(x = c(0.6, 1.85, 1.85, 3.1, 3.1), y = c(0.5, 0.3, 0.8, 0.15, 0.65), labels = format(c(2, 35, 26, 6982, 14013), big.mark = ","))

# dev.copy(png, "../pics/sejong_chungcheong_barplot.png", width = 480, height = 360)
# dev.off()

ggplot

vote.chung.class.df <- as.data.frame(vote.chung.class)
barplot.gg(vote.chung.class.df, position = "fill", base_family = "Malgun Gothic", ggtitle = "충청도의 찬반", xlab = "계급")

# ggsave("../pics/sejong_chungcheong_barplot_fill_ggplot.png", width = 6, height = 4.5, dpi = 72)

mosaic plot

수효의 차이가 너무 커서 비교하기 어려움. ‘대신 등’ 제외하고 작업

par(family = "Malgun Gothic")
mosaicplot(t(vote.chung.class[, -1]), col = rainbow.2, main = "", xlab = "계급", ylab = "찬반")
title(main = "충청도의 찬반")

# dev.copy(png, "../pics/sejong_chungcheong_mosaic.png", width = 480, height = 360)
# dev.off()

ggplot

# source("./mosaic.gg.R")
m5.chungcheong <- mosaic_gg(vote.chung.class[, -1], base_family = "Malgun Gothic", ggtitle = "충청도의 찬반", xlab = "계급")
m5.chungcheong

# ggsave("../pics/sejong_chungcheong_mosaic_ggplot.png", m5.chungcheong, width = 12, height = 4.5, dpi = 72)

자료 정리

save.image(file = "./sejong_ggplot.RData")