Advertisement
hirogami

Population_pr_0_65_script

May 15th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. # population of regions (%) by age group
  2. # call libraries
  3. library(ggplot2)
  4. library(ggthemes)
  5. library(tidyr)
  6. library(plyr)
  7. # install data
  8. data14 <- read.table("http://pastebin.com/raw/yZPV2abS", sep=",", header=T, check.names=F)
  9. data15_64 <- read.table("http://pastebin.com/raw/Afpt8KqG", header=T, sep=",", check.names=F)
  10. data65 <- read.table("http://pastebin.com/raw/WQhuR4Bz", header=T, sep=",", check.names=F)
  11. # wide to long
  12. data14.long <- gather(data14, year, rate0_14, c(3:9), factor_key=TRUE)
  13. data14.long$year <- as.numeric(as.character(data14.long$year))
  14. data15_64.long <- gather(data15_64, year, rate15_64, c(3:9), factor_key=TRUE)
  15. data15_64.long$year <- as.numeric(as.character(data15_64.long$year))
  16. data65.long <- gather(data65, year, rate65, c(3:9), factor_key=TRUE)
  17. data65.long$year <- as.numeric(as.character(data65.long$year))
  18. # choose a region
  19. chooseregion <- "青森市"
  20. # get the data
  21. data14.long.region <- data14.long %>% dplyr::filter(region == chooseregion)
  22. data15_64.long.region <- data15_64.long %>% dplyr::filter(region == chooseregion)
  23. data65.long.region <- data65.long %>% dplyr::filter(region == chooseregion)
  24. # merge the age groups
  25. dataall.long.region <- merge(data14.long.region, merge(data15_64.long.region, data65.long.region))
  26. # print the data of the city
  27. dataall.long.region
  28. # wide to long
  29. dataall.long.region <- gather(dataall.long.region, group, rate, c(4:6), factor_key=TRUE)
  30. # plot
  31. p1 = ggplot(aes(y = rate, x = year, fill = group), data = dataall.long.region) +
  32. geom_area()+
  33. ggtitle(chooseregion) +
  34. labs(x="Year",y="percentage") +
  35. theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0)) +
  36. theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=14)) +
  37. theme_bw(base_family = "HiraKakuProN-W3")
  38. p1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement