# population of regions in japan by age group (%)
# call libraries
library(ggplot2)
library(ggthemes)
library(extrafont)
## Registering fonts with R
library(tidyr)
library(plyr)
# install data
data14 <- read.table("http://pastebin.com/raw/yZPV2abS", sep=",", header=T, check.names=F)
data15_64 <- read.table("http://pastebin.com/raw/Afpt8KqG", header=T, sep=",", check.names=F)
data65 <- read.table("http://pastebin.com/raw/WQhuR4Bz", header=T, sep=",", check.names=F)
# wide to long
data14.long <- gather(data14, year, rate0_14, c(3:9), factor_key=TRUE)
data14.long$year <- as.numeric(as.character(data14.long$year)) 
data15_64.long <- gather(data15_64, year, rate15_64, c(3:9), factor_key=TRUE)
data15_64.long$year <- as.numeric(as.character(data15_64.long$year)) 
data65.long <- gather(data65, year, rate65, c(3:9), factor_key=TRUE)
data65.long$year <- as.numeric(as.character(data65.long$year)) 
# choose a region
chooseregion <- "鳥羽市"
# get the data
data14.long.region <- data14.long %>% dplyr::filter(region == chooseregion)
data15_64.long.region <- data15_64.long %>% dplyr::filter(region == chooseregion)
data65.long.region <- data65.long %>% dplyr::filter(region == chooseregion)
# merge the age groups
dataall.long.region <- merge(data14.long.region, merge(data15_64.long.region, data65.long.region))
# print the data of the city
dataall.long.region
##      id region year rate0_14 rate15_64 rate65
## 1 24211 鳥羽市 2010     11.5      58.7   29.8
## 2 24211 鳥羽市 2015      9.9      55.8   34.3
## 3 24211 鳥羽市 2020      8.7      53.3   38.1
## 4 24211 鳥羽市 2025      8.0      51.4   40.7
## 5 24211 鳥羽市 2030      7.5      49.1   43.4
## 6 24211 鳥羽市 2035      7.4      47.7   44.9
## 7 24211 鳥羽市 2040      7.4      46.5   46.1
# wide to long
dataall.long.region <- gather(dataall.long.region, group, rate, c(4:6), factor_key=TRUE)
# plot 
p1 = ggplot(aes(y = rate, x = year, fill = group), data = dataall.long.region) +
  geom_area() +
  geom_area() + geom_line(position="stack", size = 0.3) +
  ggtitle(chooseregion) +
  labs(x="Year",y="percentage") + 
  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