Código

# Pacotes ----------------------------------------------------------------------

library(tidyverse)
library(gganimate)
library(lubridate)
library(scales)
library(spcrimr)

# seleciona populacao-----------------------------------------------------------
pop <- seade %>% select(chave, municipio, ano, populacao)

# seleciona crimes -------------------------------------------------------------
crim <- 
  inner_join(ssp_ocorrencias_crimes,
             ssp_produtividade_policial,
             by = c("chave", "municipio", "ano")) %>%
  select(chave:ano,
         homicidio_doloso,
         armas_de_fogo_apreendidas, 
         ocorrências_de_porte_ilegal_de_arma)

# prepara para análise ---------------------------------------------------------
arma_homic <- 
  # junta bases e agrupa municipios
  inner_join(pop, crim, by = c("chave", "municipio", "ano")) %>%
  group_by(ano) %>%
  summarise_at(vars(populacao:ocorrências_de_porte_ilegal_de_arma), ~ sum(.)) %>%
  ungroup() %>%
  # calcula taxa
  mutate_at(.vars = vars(homicidio_doloso:ocorrências_de_porte_ilegal_de_arma),
            .funs = ~ . / populacao * 100000) %>%
  select(-populacao) %>%
  # empilha
  gather(key = nome, value = taxa, -ano, factor_key = TRUE) %>%
  # melhora o nome das variáveis
  mutate(
    nome = fct_recode(nome,
                     "Homicídio" = "homicidio_doloso",
                     "Armas de fogo apreendidas" = "armas_de_fogo_apreendidas",
                     "Ocorrências porte ilegal de arma" = "ocorrências_de_porte_ilegal_de_arma")
  )


# Constrói o gráfico -----------------------------------------------------------
options(gganimate.dev_args = list(width = 600, height = 500))

ggplot(arma_homic, aes(x = as.integer(ano), y = taxa, group = nome)) +
  # linhas -----------------------------------------------------------
  geom_line(aes(color = nome,
                xend = 2025),
                size = 2,
                show.legend = FALSE) +
  
  # linha horizontal pontilhada --------------------------------------
  geom_segment(aes(xend = 2017,
                   yend = taxa,
                   colour = nome),
               show.legend = F,
               linetype = 2) +

  # ponto -------------------------------------------------------------
  geom_point(size=2) +
  # labels ------------------------------------------------------------
     # nome da variável
  geom_text(aes(x = 2017.1,
                label = nome,
                color = nome),
            show.legend = F,
            size = 6,
            vjust = 1,
            hjust = 0) + 
     # valor
  geom_text(aes(x = 2017.1,
                label = sprintf("%1.2f", taxa),
                color = nome),
            show.legend = F,
            size = 6,
            vjust = -.25,
            hjust = 0) + 
     # est. desarm.
  geom_text(aes(label="Estatuto do desarmamento -  Lei 10.826"),
            angle = 90,
            y = 71,
            x = 2003,
            size = 4.5,
            vjust = 1) + 
     # labs
  labs(title = "Mais armas, mais homicídios",
       subtitle="Estado de São Paulo (2002-2017) - Taxas por 100.000 habitantes",
       x="",
       y="") +
  # linhas verticais ------------------------------------------------
    # Estatuto desarmamento
  geom_vline(xintercept = 2003,
             linetype = "dotted",
             size = 1,
             color = "darkorange") +
    # Linha de acompanhamento
  geom_vline(aes(xintercept = as.integer(ano)),
             linetype = "dotted",
             color = "grey80") +
  # efeito gganimate ------------------------------------------------
  transition_reveal(ano) +
  coord_cartesian(clip="off") + 
  # tema e estilo ---------------------------------------------------
  theme(plot.margin = margin(25, 75, 25, 25),
        axis.title = element_text(size = 15,face = 'bold'),
        axis.text = element_text(size = 12),
        axis.ticks.y = element_blank(),
        axis.ticks.length = unit(.25, "cm"),
        plot.title = element_text(vjust = .5, hjust = .5, size = 17),
        plot.subtitle = element_text(vjust = .5, hjust = .5, size = 13),
        axis.text.x = element_text(angle = 45, hjust=1, vjust=1),
        panel.background = element_rect(fill = "azure",colour = "azure", size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "azure2"), 
        panel.grid.minor = element_line(size = 0.5, linetype = 'solid', colour = "azure2")) +
  # escalas ----------------------------------------------------------
     # para os anos
  scale_x_discrete(limits = seq(2002, 2017),
                   breaks = seq(2002, 2017),
                   labels = seq(2002, 2017)) +
    # cores das linhas
  scale_color_manual(values=c("red", "darkred", "grey27", "black")) +
    # valores eixo y
  scale_y_continuous(breaks=seq(0,125, by=12.5))