SHARE
TWEET

PopulationScriptShowTableAndPlot

hirogami May 18th, 2017 (edited) 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Script population of regions in japan by age group (%)
  2. # call libraries
  3. library(ggplot2)
  4. library(ggthemes)
  5. library(tidyr)
  6. library(plyr)
  7. # wide to long
  8. data14.long <- gather(data14, year, rate0_14, c(3:9), factor_key=TRUE)
  9. data14.long$year <- as.numeric(as.character(data14.long$year))
  10. data15_64.long <- gather(data15_64, year, rate15_64, c(3:9), factor_key=TRUE)
  11. data15_64.long$year <- as.numeric(as.character(data15_64.long$year))
  12. data65.long <- gather(data65, year, rate65, c(3:9), factor_key=TRUE)
  13. data65.long$year <- as.numeric(as.character(data65.long$year))
  14. # choose a region
  15. data14.long.region <- data14.long %>% dplyr::filter(region == "桑名市")
  16. data15_64.long.region <- data15_64.long %>% dplyr::filter(region == "桑名市")
  17. data65.long.region <- data65.long %>% dplyr::filter(region == "桑名市")
  18. # merge the age groups
  19. dataall.long.region <- merge(data14.long.region, merge(data15_64.long.region, data65.long.region))
  20. # print the data of the city
  21. dataall.long.region
  22. # wide to long
  23. dataall.long.region <- gather(dataall.long.region, group, rate, c(4:6), factor_key=TRUE)
  24. # plot
  25. p1 = ggplot(aes(y = rate, x = year, fill = group), data = dataall.long.region) +
  26.      geom_area()
  27. p1
RAW Paste Data
Top