Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- r piramide, include = FALSE}
- label_sexo <- c('0' = "Masculino",'1' = "Feminino")
- idades <- base_matriculas_final[,list(matriculados =
- length(unique(id_aluno))),
- by = c("vila","sexo_adj","idade")]
- base_matriculas_final[,sexo_label := sexo_adj %>% factor(labels =
- label_sexo)]
- base_matriculas_final[,idade_5 := (idade %/% 5)*5]
- idades_prop <- base_matriculas_final[idade_5 <= 70,
- list(N = length(unique(id_aluno))),
- by =
- c("sexo_label","vila","idade_5","sexo_adj")]
- idades_prop <- idades_prop[,prop := 100*ifelse(sexo_label =="Masculino",
- -N/sum(N),
- N/sum(N)),
- by = vila]
- max_prop <- idades_prop[,max(abs(prop),na.rm = T)]
- max_prop <- ifelse(max_prop > 20, 25, 20)
- piramides <-
- lapply(split(idades_prop, by = "vila"),
- function(data){
- vila_n <- data[,unique(vila)]
- ggplot(data,
- aes(x = idade_5,y = prop,
- geom_bar(data = subset(data,sexo_adj == 1),
- stat = "identity") +
- geom_bar(data = subset(data,sexo_adj == 0),
- stat = "identity") +
- geom_text(aes(label = format (prop, digits=2)),
- hjust = "outward", size = 2,
- position = position_dodge(width = 1), check_overlap =
- TRUE) +
- scale_y_continuous(labels = abs,
- limits = max_prop*c(-1,1),
- breaks = seq(-max_prop, max_prop, by = 5)) +
- scale_x_continuous(expand=c(0,0), breaks = seq(0,120, by = 5))
- +
- coord_flip() +
- labs(title = vila_n,
- y = "% de pessoas",
- x = "Idade (5 em 5 anos)",
- fill = "Gênero") -> plot_vila
- plot_vila
- })
- + theme_bw()
Add Comment
Please, Sign In to add comment