Advertisement
hirogami

Population_region_number_script

May 21st, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.28 KB | None | 0 0
  1. # population of regions in japan by age group (%)
  2. # call libraries
  3. library(ggplot2)
  4. library(ggthemes)
  5. library(extrafont)
  6. library(tidyr)
  7. library(plyr)
  8. # install data
  9. popdata14 <- read.table("https://pastebin.com/raw/Ef8VzNWD", sep=",", header=T, check.names=F)
  10. popdata15_64 <- read.table("https://pastebin.com/raw/DcufnuA7", header=T, sep=",", check.names=F)
  11. popdata65 <- read.table("https://pastebin.com/raw/e3V3jNfc", header=T, sep=",", check.names=F)
  12. # wide to long
  13. popdata14.long <- gather(popdata14, year, pop0_14, c(3:9), factor_key=TRUE)
  14. popdata14.long$year <- as.numeric(as.character(popdata14.long$year))
  15. popdata15_64.long <- gather(popdata15_64, year, pop15_64, c(3:9), factor_key=TRUE)
  16. popdata15_64.long$year <- as.numeric(as.character(popdata15_64.long$year))
  17. popdata65.long <- gather(popdata65, year, pop65, c(3:9), factor_key=TRUE)
  18. popdata65.long$year <- as.numeric(as.character(popdata65.long$year))
  19. # choose a region
  20. chooseregion <- "志摩市"
  21. # get the data
  22. popdata14.long.region <- popdata14.long %>% dplyr::filter(region == chooseregion)
  23. popdata15_64.long.region <- popdata15_64.long %>% dplyr::filter(region == chooseregion)
  24. popdata65.long.region <- popdata65.long %>% dplyr::filter(region == chooseregion)
  25. # merge the age groups
  26. popdataall.long.region <- merge(popdata14.long.region, merge(popdata15_64.long.region, popdata65.long.region))
  27. # print the data of the city
  28. popdataall.long.region
  29.  # population in 2015
  30.  pop2015.group <-
  31.    popdataall.long.region %>%
  32.    dplyr::filter(year == "2015")
  33.  pop2015.group
  34.  pop2015.group.long <- gather(pop2015.group, group, population, c(4:6), factor_key=TRUE)
  35.  pop2015 <- sum(pop2015.group.long$population)
  36. # print poplulation 2015
  37.   pop2015
  38. # wide to long
  39. popdataall.long.region <- gather(popdataall.long.region, group, population, c(4:6), factor_key=TRUE)
  40. # plot
  41. p1 = ggplot(aes(y = population, x = year, fill = group), data = popdataall.long.region) +
  42.   geom_area() +
  43.   ggtitle(paste(chooseregion, "\n", pop2015, "(2015)")) +
  44.   labs(x="year",y="population") +
  45.   theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0)) +
  46.   theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=14)) +
  47.   theme_bw(base_family = "HiraKakuProN-W3")
  48. p1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement