Guest User

Untitled

a guest
Mar 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. r piramide, include = FALSE}
  2. label_sexo <- c('0' = "Masculino",'1' = "Feminino")
  3.  
  4. idades <- base_matriculas_final[,list(matriculados =
  5. length(unique(id_aluno))),
  6. by = c("vila","sexo_adj","idade")]
  7. base_matriculas_final[,sexo_label := sexo_adj %>% factor(labels =
  8. label_sexo)]
  9. base_matriculas_final[,idade_5 := (idade %/% 5)*5]
  10.  
  11. idades_prop <- base_matriculas_final[idade_5 <= 70,
  12. list(N = length(unique(id_aluno))),
  13. by =
  14. c("sexo_label","vila","idade_5","sexo_adj")]
  15.  
  16. idades_prop <- idades_prop[,prop := 100*ifelse(sexo_label =="Masculino",
  17. -N/sum(N),
  18. N/sum(N)),
  19. by = vila]
  20. max_prop <- idades_prop[,max(abs(prop),na.rm = T)]
  21. max_prop <- ifelse(max_prop > 20, 25, 20)
  22.  
  23. piramides <-
  24. lapply(split(idades_prop, by = "vila"),
  25. function(data){
  26. vila_n <- data[,unique(vila)]
  27. ggplot(data,
  28. aes(x = idade_5,y = prop,
  29. geom_bar(data = subset(data,sexo_adj == 1),
  30. stat = "identity") +
  31. geom_bar(data = subset(data,sexo_adj == 0),
  32. stat = "identity") +
  33. geom_text(aes(label = format (prop, digits=2)),
  34. hjust = "outward", size = 2,
  35. position = position_dodge(width = 1), check_overlap =
  36. TRUE) +
  37. scale_y_continuous(labels = abs,
  38. limits = max_prop*c(-1,1),
  39. breaks = seq(-max_prop, max_prop, by = 5)) +
  40. scale_x_continuous(expand=c(0,0), breaks = seq(0,120, by = 5))
  41. +
  42. coord_flip() +
  43. labs(title = vila_n,
  44. y = "% de pessoas",
  45. x = "Idade (5 em 5 anos)",
  46. fill = "Gênero") -> plot_vila
  47. plot_vila
  48. })
  49. + theme_bw()
Add Comment
Please, Sign In to add comment