Problem

1769년(영조 45년) 도별 수조 현황표를 막대그래프로 표시

Data Setup

library(extrafont)
## Registering fonts with R
province <- c("경기도", "충청도", "전라도", "경상도", "강원도", "황해도", "함경도", "평안도")
expected <- c(9939, 35476, 69692, 60399, 3229, 29883, 5167, 31996)
collected <- c(8620, 31657, 67277, 25283, 3532, 27265, 0, 0)
tax.df <- data.frame(province, expected, collected, stringsAsFactors = FALSE)
tax.df
##   province expected collected
## 1   경기도     9939      8620
## 2   충청도    35476     31657
## 3   전라도    69692     67277
## 4   경상도    60399     25283
## 5   강원도     3229      3532
## 6   황해도    29883     27265
## 7   함경도     5167         0
## 8   평안도    31996         0

Barplot(R Base)

library(extrafont)
par(family = "HCR Dotum LVT")
b1 <- barplot(t(as.matrix(tax.df[, 2:3])), 
        axes = FALSE, 
        ylim = c(0, 80000), 
        beside = TRUE, 
        names.arg = tax.df[, 1], 
        legend.text = c("징수액", "상납액"), col = c("darkgrey", "blue"))
# axis(side = 2, 
#     at = as.vector(as.matrix(rates.df[, 2:3])), 
#     labels =  as.vector(as.matrix(rates.df[, 2:3])), las = 1)
text(x = b1[1, ], y = expected + 2000, labels = format(expected, big.mark = ","), col = "black")
text(x = b1[2, ], y = collected + 2000, labels = format(collected, big.mark = ","), col = "black")
main.title <- "영조 45년 도별 수조 현황"
sub.title <- "증보문헌비고, 제149권 전부고 9 조세 2 (영조 45년, 1769년)"
main.text <- "쌀환산(석)\n상납비율은 약 66%"
title(main = main.title, sub = sub.title, cex.main = 2)
text(x = 18, y = 60000, main.text, cex = 1.6, adj = 0.5)

ggplot

Data for ggplot

library(reshape2)
tax.df$province.f <- factor(province, levels = province)
tax.df
##   province expected collected province.f
## 1   경기도     9939      8620     경기도
## 2   충청도    35476     31657     충청도
## 3   전라도    69692     67277     전라도
## 4   경상도    60399     25283     경상도
## 5   강원도     3229      3532     강원도
## 6   황해도    29883     27265     황해도
## 7   함경도     5167         0     함경도
## 8   평안도    31996         0     평안도
str(tax.df)
## 'data.frame':    8 obs. of  4 variables:
##  $ province  : chr  "경기도" "충청도" "전라도" "경상도" ...
##  $ expected  : num  9939 35476 69692 60399 3229 ...
##  $ collected : num  8620 31657 67277 25283 3532 ...
##  $ province.f: Factor w/ 8 levels "경기도","충청도",..: 1 2 3 4 5 6 7 8
tax.df.melt <- melt(tax.df[, 2:4], 
                      id.vars = "province.f", 
                      measure.vars = c("expected", "collected"), 
                      variable.name = "tax", value.name = "amount")
tax.df.melt
##    province.f       tax amount
## 1      경기도  expected   9939
## 2      충청도  expected  35476
## 3      전라도  expected  69692
## 4      경상도  expected  60399
## 5      강원도  expected   3229
## 6      황해도  expected  29883
## 7      함경도  expected   5167
## 8      평안도  expected  31996
## 9      경기도 collected   8620
## 10     충청도 collected  31657
## 11     전라도 collected  67277
## 12     경상도 collected  25283
## 13     강원도 collected   3532
## 14     황해도 collected  27265
## 15     함경도 collected      0
## 16     평안도 collected      0
str(tax.df.melt)
## 'data.frame':    16 obs. of  3 variables:
##  $ province.f: Factor w/ 8 levels "경기도","충청도",..: 1 2 3 4 5 6 7 8 1 2 ...
##  $ tax       : Factor w/ 2 levels "expected","collected": 1 1 1 1 1 1 1 1 2 2 ...
##  $ amount    : num  9939 35476 69692 60399 3229 ...

Geom_bar

library(ggplot2)
source("./theme_kr.R")
g0 <- ggplot(data = tax.df.melt, 
             mapping = aes(x = province.f, y = amount, fill = tax)) 
(g1 <- g0 + 
  geom_bar(stat = "identity", position = position_dodge())) 

# g1
g2 <- g1 +
  geom_text(mapping = aes(x = province.f, 
                          y = amount + 2000, 
                          label = format(amount, big.mark = ","),
                          colour = tax), 
            position = position_dodge(width = 1), 
            size = 3)
g2

g3 <- g2 +
  theme_bw() +
  theme.kr
g3

g4 <- g3 + 
    scale_fill_manual(name = "", 
                      values = c("darkgrey", "blue"), 
                      labels = c("징수액", "상납액")) +
    scale_colour_manual(name = "",
                        values = c("darkgrey", "blue"), 
                        labels = c("징수액", "상납액"))
g4

g5 <- g4 + 
    scale_x_discrete(name = "지역")
g5

g6 <- g5 +
    scale_y_continuous(name = "쌀환산(석)", 
                       breaks = as.vector(as.matrix(tax.df[, 2])), 
                       labels = format(as.vector(as.matrix(tax.df[, 2])), big.mark = ","))
g6

g7 <- g6 +
    labs(title = main.title, subtitle = sub.title)
g7

g8 <- g7 +
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(family = "HCR Dotum LVT"),
        legend.position = c(0.9, 0.8))
g8

ggsave("../pics/chosun_tax.png", dpi = 72)
## Saving 8 x 4 in image