조선시대 전답 통계를 stacked area graph로 표시
options(warn = -1)
library(knitr)
include_graphics("../pics/chosun_field_history.png", dpi = 72)
library(knitr)
year <- c(1404, 1414, 1432, 1592, 1603, 1634, 1719, 1784, 1786, 1807, 1864)
province <- c("경기도", "충청도", "전라도", "경상도", "황해도", "강원도", "함경도", "평안도")
field <- matrix(c(NA, 223090, 173990, 224625, 90922, 59989, 3271, 6648,
NA, 223090, 279090, 226025, 90925, 59989, 3271, 6648,
207119, 236300, 377588, 301147, 104772, 65916, 130413, 308751,
147370, 250503, 442189, 315026, 106832, 34831, 63821, 153009,
141959, 240744, 198672, 173902, 108211, 33884, 54377, 153009,
100359, 258461, 335305, 301819, 128834, 33884, 61243, 94000,
101256, 255208, 377159, 336778, 128834, 44051, 61243, 90804,
110932, 255519, 348489, 335730, 129244, 40889, 109556, 105760,
109932, 255519, 348489, 336730, 129244, 40889, 109556, 106041,
112090, 256528, 340103, 337128, 132211, 41151, 117746, 119635,
111912, 255585, 339743, 337472, 132373, 40926, 117746, 119735),
ncol = 8,
byrow = TRUE)
rownames(field) <- year
colnames(field) <- province
str(field)
## num [1:11, 1:8] NA NA 207119 147370 141959 ...
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:11] "1404" "1414" "1432" "1592" ...
## ..$ : chr [1:8] "경기도" "충청도" "전라도" "경상도" ...
options(digits = 2)
mean.field <- colMeans(field, na.rm = TRUE)
prop.field <- colMeans(field, na.rm = TRUE)/sum(colMeans(field, na.rm = TRUE)) * 100
matrix
(array)에 특화된 melt
사용
library(reshape2)
field.melt <- melt(field, varnames = c("Year", "Province"), value.name = "Area")
kable(field.melt)
Year | Province | Area |
---|---|---|
1404 | 경기도 | NA |
1414 | 경기도 | NA |
1432 | 경기도 | 207119 |
1592 | 경기도 | 147370 |
1603 | 경기도 | 141959 |
1634 | 경기도 | 100359 |
1719 | 경기도 | 101256 |
1784 | 경기도 | 110932 |
1786 | 경기도 | 109932 |
1807 | 경기도 | 112090 |
1864 | 경기도 | 111912 |
1404 | 충청도 | 223090 |
1414 | 충청도 | 223090 |
1432 | 충청도 | 236300 |
1592 | 충청도 | 250503 |
1603 | 충청도 | 240744 |
1634 | 충청도 | 258461 |
1719 | 충청도 | 255208 |
1784 | 충청도 | 255519 |
1786 | 충청도 | 255519 |
1807 | 충청도 | 256528 |
1864 | 충청도 | 255585 |
1404 | 전라도 | 173990 |
1414 | 전라도 | 279090 |
1432 | 전라도 | 377588 |
1592 | 전라도 | 442189 |
1603 | 전라도 | 198672 |
1634 | 전라도 | 335305 |
1719 | 전라도 | 377159 |
1784 | 전라도 | 348489 |
1786 | 전라도 | 348489 |
1807 | 전라도 | 340103 |
1864 | 전라도 | 339743 |
1404 | 경상도 | 224625 |
1414 | 경상도 | 226025 |
1432 | 경상도 | 301147 |
1592 | 경상도 | 315026 |
1603 | 경상도 | 173902 |
1634 | 경상도 | 301819 |
1719 | 경상도 | 336778 |
1784 | 경상도 | 335730 |
1786 | 경상도 | 336730 |
1807 | 경상도 | 337128 |
1864 | 경상도 | 337472 |
1404 | 황해도 | 90922 |
1414 | 황해도 | 90925 |
1432 | 황해도 | 104772 |
1592 | 황해도 | 106832 |
1603 | 황해도 | 108211 |
1634 | 황해도 | 128834 |
1719 | 황해도 | 128834 |
1784 | 황해도 | 129244 |
1786 | 황해도 | 129244 |
1807 | 황해도 | 132211 |
1864 | 황해도 | 132373 |
1404 | 강원도 | 59989 |
1414 | 강원도 | 59989 |
1432 | 강원도 | 65916 |
1592 | 강원도 | 34831 |
1603 | 강원도 | 33884 |
1634 | 강원도 | 33884 |
1719 | 강원도 | 44051 |
1784 | 강원도 | 40889 |
1786 | 강원도 | 40889 |
1807 | 강원도 | 41151 |
1864 | 강원도 | 40926 |
1404 | 함경도 | 3271 |
1414 | 함경도 | 3271 |
1432 | 함경도 | 130413 |
1592 | 함경도 | 63821 |
1603 | 함경도 | 54377 |
1634 | 함경도 | 61243 |
1719 | 함경도 | 61243 |
1784 | 함경도 | 109556 |
1786 | 함경도 | 109556 |
1807 | 함경도 | 117746 |
1864 | 함경도 | 117746 |
1404 | 평안도 | 6648 |
1414 | 평안도 | 6648 |
1432 | 평안도 | 308751 |
1592 | 평안도 | 153009 |
1603 | 평안도 | 153009 |
1634 | 평안도 | 94000 |
1719 | 평안도 | 90804 |
1784 | 평안도 | 105760 |
1786 | 평안도 | 106041 |
1807 | 평안도 | 119635 |
1864 | 평안도 | 119735 |
library(ggplot2)
# library(extrafont)
source("./theme_kr.R")
g0 <- ggplot()
g1 <- g0 +
geom_area(data = field.melt,
mapping = aes(x = Year, y = Area, fill = Province),
colour = "black",
size = 0.2,
alpha = 0.4,
na.rm = TRUE)
g1
g2 <- g1 +
scale_fill_brewer(palette = "Spectral",
name = "",
breaks = levels(field.melt$Province)) +
theme_bw() +
theme.kr
g2
g3<- g2 +
scale_x_continuous(name = "연도",
breaks = as.numeric(row.names(field)[-9]),
labels = row.names(field)[-9])
g3
g4 <- g3 +
theme(axis.text.x = element_text(angle = 90))
g4
g5 <- g4 +
scale_y_continuous(name = "토지 면적(결)",
breaks = cumsum(rev(field[3, ])),
labels = format(cumsum(rev(field[3, ])), big.mark = ","))
g5
g6 <- g5 +
labs(title = "조선 시대 도별 논밭통계", subtitle = "(도표 안의 수치는 기록된 값들의 평균)") +
theme(plot.subtitle = element_text(family = "HCR Dotum LVT", hjust = 1))
g6
g7 <- g6 +
theme(plot.title = element_text(size = 16, hjust = 0.5))
g7
# x.max <- max(year) + 0.15 * diff(range(year))
# g8 <- g7 +
# scale_x_continuous(name = "연도",
# breaks = as.numeric(row.names(field)[-9]),
# labels = row.names(field)[-9],
# limits = c(min(year), x.max)) +
# theme(legend.position = c(0.95, 0.5),
# legend.box.background = element_rect(fill = "white", colour = "black"),
# legend.title = element_blank())
# g8
g9 <- g7 +
theme(legend.position = c(0.75, 0.9),
legend.box.background = element_rect(fill = "white", colour = "black"),
legend.direction = "horizontal")
g9
y.coord <- apply(field[3:4, ], 2, mean)
y.text <- cumsum(c(0, head(rev(y.coord), -1))) + rev(y.coord) / 2
mean.text <- rev(paste(province,
":",
format(mean.field, big.mark = ","),
"결(",
format(prop.field, digits = 2, nsmall = 0),
"%)",
sep = "" ))
text.df <- data.frame(x = (year[3] + year[4]) / 2,
y = y.text,
label = mean.text,
row.names = NULL,
stringsAsFactors = FALSE)
g10 <- g9 +
guides(fill = FALSE) +
geom_text(data = text.df,
mapping = aes(x = x, y = y),
label = mean.text,
family = "HCR Dotum LVT", size = 3) +
annotate("text", x = 1592, y = 1650000,
label = "임진왜란",
colour = "red",
size = 5,
family = "HCR Dotum LVT")
# theme(text = element_text(family = "HCR Dotum LVT"))
# annotate("text",
# x = text.df$x,
# y = text.df$y,
# label = mean.text,
# family = "HCR Dotum LVT")
g10
ggsave("../pics/chosun_field_ggplot.png", dpi = 72)