Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # population of regions in japan by age group (%)
- # call libraries
- library(ggplot2)
- library(ggthemes)
- library(extrafont)
- library(tidyr)
- library(plyr)
- # 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
- chooseregion <- "志摩市"
- # 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
- # population in 2015
- pop2015.group <-
- popdataall.long.region %>%
- dplyr::filter(year == "2015")
- pop2015.group
- pop2015.group.long <- gather(pop2015.group, group, population, c(4:6), factor_key=TRUE)
- pop2015 <- sum(pop2015.group.long$population)
- # print poplulation 2015
- pop2015
- # wide to long
- popdataall.long.region <- gather(popdataall.long.region, group, population, c(4:6), factor_key=TRUE)
- # plot
- p1 = ggplot(aes(y = population, x = year, fill = group), data = popdataall.long.region) +
- geom_area() +
- ggtitle(paste(chooseregion, "\n", pop2015, "(2015)")) +
- labs(x="year",y="population") +
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement