# population index of regions in japan by age group 2010=100
# call libraries
library(ggplot2)
library(ggthemes)
library(extrafont)
## Registering fonts with R
library(tidyr)
library(plyr)
# install data
data14.index <- read.table("https://pastebin.com/raw/zzwgYCKs", sep=",", header=T, check.names=F)
data15_64.index <- read.table("https://pastebin.com/raw/8SQPMeD8", header=T, sep=",", check.names=F)
data65.index <- read.table("https://pastebin.com/raw/tD0hVfvu", header=T, sep=",", check.names=F)
# wide to long
data14.index.long <- gather(data14.index, year, pop0_14, c(3:9), factor_key=TRUE)
data14.index.long$year <- as.numeric(as.character(data14.index.long$year))
data15_64.index.long <- gather(data15_64.index, year, pop15_64, c(3:9), factor_key=TRUE)
data15_64.index.long$year <- as.numeric(as.character(data15_64.index.long$year))
data65.index.long <- gather(data65.index, year, pop65, c(3:9), factor_key=TRUE)
data65.index.long$year <- as.numeric(as.character(data65.index.long$year))
# choose a region
chooseregion.index <- "今治市"
# get the data
data14.index.long.region <- data14.index.long %>% dplyr::filter(region == chooseregion.index)
data15_64.index.long.region <- data15_64.index.long %>% dplyr::filter(region == chooseregion.index)
data65.index.long.region <- data65.index.long %>% dplyr::filter(region == chooseregion.index)
# merge the age groups
dataall.index.long.region <- merge(data14.index.long.region, merge(data15_64.index.long.region, data65.index.long.region))
# print the data of the city
dataall.index.long.region
## id region year pop0_14 pop15_64 pop65
## 1 38202 今治市 2010 100.0 100.0 100.0
## 2 38202 今治市 2015 88.9 88.8 110.5
## 3 38202 今治市 2020 78.5 81.5 112.5
## 4 38202 今治市 2025 68.6 76.4 108.5
## 5 38202 今治市 2030 60.1 71.5 102.8
## 6 38202 今治市 2035 54.4 65.7 97.5
## 7 38202 今治市 2040 50.4 58.2 95.4
# wide to long
dataall.index.long.region.long <- gather(dataall.index.long.region, group, index, c(4:6), factor_key=TRUE)
dataall.index.long.region.long
## id region year group index
## 1 38202 今治市 2010 pop0_14 100.0
## 2 38202 今治市 2015 pop0_14 88.9
## 3 38202 今治市 2020 pop0_14 78.5
## 4 38202 今治市 2025 pop0_14 68.6
## 5 38202 今治市 2030 pop0_14 60.1
## 6 38202 今治市 2035 pop0_14 54.4
## 7 38202 今治市 2040 pop0_14 50.4
## 8 38202 今治市 2010 pop15_64 100.0
## 9 38202 今治市 2015 pop15_64 88.8
## 10 38202 今治市 2020 pop15_64 81.5
## 11 38202 今治市 2025 pop15_64 76.4
## 12 38202 今治市 2030 pop15_64 71.5
## 13 38202 今治市 2035 pop15_64 65.7
## 14 38202 今治市 2040 pop15_64 58.2
## 15 38202 今治市 2010 pop65 100.0
## 16 38202 今治市 2015 pop65 110.5
## 17 38202 今治市 2020 pop65 112.5
## 18 38202 今治市 2025 pop65 108.5
## 19 38202 今治市 2030 pop65 102.8
## 20 38202 今治市 2035 pop65 97.5
## 21 38202 今治市 2040 pop65 95.4
 # install data
 popdata14 <- read.table("https://pastebin.com/raw/Ef8VzNWD", sep=",", header=T, check.names=F)
 popdata15_64 <- read.table("https://pastebin.com/raw/DcufnuA7", header=T, sep=",", check.names=F)
 popdata65 <- read.table("https://pastebin.com/raw/e3V3jNfc", header=T, sep=",", check.names=F)
 # wide to long
 popdata14.long <- gather(popdata14, year, pop0_14, c(3:9), factor_key=TRUE)
 popdata14.long$year <- as.numeric(as.character(popdata14.long$year))
 popdata15_64.long <- gather(popdata15_64, year, pop15_64, c(3:9), factor_key=TRUE)
 popdata15_64.long$year <- as.numeric(as.character(popdata15_64.long$year))
 popdata65.long <- gather(popdata65, year, pop65, c(3:9), factor_key=TRUE)
 popdata65.long$year <- as.numeric(as.character(popdata65.long$year))
 # choose a region for 2010
 chooseregion <- chooseregion.index
 # get the data
 popdata14.long.region <- popdata14.long %>% dplyr::filter(region == chooseregion)
 popdata15_64.long.region <- popdata15_64.long %>% dplyr::filter(region == chooseregion)
 popdata65.long.region <- popdata65.long %>% dplyr::filter(region == chooseregion)
 # merge the age groups
 popdataall.long.region <- merge(popdata14.long.region, merge(popdata15_64.long.region, popdata65.long.region))
 # print the data of the city
 popdataall.long.region
## id region year pop0_14 pop15_64 pop65
## 1 38202 今治市 2010 20844 97831 47857
## 2 38202 今治市 2015 18523 86891 52873
## 3 38202 今治市 2020 16366 79777 53821
## 4 38202 今治市 2025 14296 74775 51903
## 5 38202 今治市 2030 12524 69959 49197
## 6 38202 今治市 2035 11330 64285 46677
## 7 38202 今治市 2040 10510 56905 45656
# population in 2010
pop2010.group <-
popdataall.long.region %>%
dplyr::filter(year == "2010")
pop2010.group
## id region year pop0_14 pop15_64 pop65
## 1 38202 今治市 2010 20844 97831 47857
pop2010.group.long <- gather(pop2010.group, group, population, c(4:6), factor_key=TRUE)
pop2010 <- sum(pop2010.group.long$population)
# print poplulation 2010
pop2010
## [1] 166532
# plot
p1 = ggplot(aes(y = index, x = year, fill = group), data = dataall.index.long.region.long) +
geom_area() +
ggtitle(paste(chooseregion.index, "\n", pop2010, "(2010)")) +
labs(x="year", y="index") +
theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0)) +
theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=14)) +
theme_bw(base_family = "HiraKakuProN-W3")
p1
