library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(ggplot2)
library(plotly)
## 
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#file.choose()
Sinies_nacional<-read_excel("C:\\Users\\ADMIN.PC_ADMIN\\Downloads\\Copia de Siniestralidad_2018_2025p(1).xlsx")
names(Sinies_nacional)
##  [1] "CodigoDepartamentoHecho" "CodigoMunicipioHecho"   
##  [3] "Departamento"            "Municipio"              
##  [5] "EstadoVictima"           "AnoHecho"               
##  [7] "Mes"                     "MesOCurrencia"          
##  [9] "Dia"                     "DiaOcurrencia"          
## [11] "TipoVehiculo"            "TipoVehiculoGrupo"      
## [13] "TipoServicio"            "CondicionVictima"       
## [15] "UsuarioVia"              "Zona"                   
## [17] "ObjetoColision"          "Rango3horas"            
## [19] "Rango6horas"             "Sexo"                   
## [21] "RangoEdad"               "ClaseAccidente"         
## [23] "Hipotesis"               "DireccionHecho"
#---------------------
mun_ten_died= Sinies_nacional%>%
  group_by(Departamento,Municipio)%>%
  summarise(Total_sinister=n(),.groups ="drop" )%>%
  arrange(desc(Total_sinister))%>%
  top_n(10,Total_sinister)

plot_ly(
  mun_ten_died,
  x = ~Municipio,
  y = ~Departamento,  
  z = ~Total_sinister, 
  type = "scatter3d",
  marker = list(
    color = ~Total_sinister,
    colorscale = "Viridis"
  )
) %>%
  layout(
    scene = list(
      xaxis = list(title = "Municipio"),
      yaxis = list(title = "Departamento"),
      zaxis = list(title = "Total de Siniestros") 
    ),
    title = "Top 5 de Siniestros Fallecidos acumulado por Departamento y Municipio (2018-2025)"
  )
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode