1. Importar shape de comunas.

require(raster)
## Cargando paquete requerido: raster
## Cargando paquete requerido: sp
require(sf)
## Cargando paquete requerido: sf
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
comunas = st_read("C:/Users/EQUIPO/Documents/Act1_AnalisisEspacial/Casos/cali/comunas.shp")
## Reading layer `Comunas' from data source 
##   `C:\Users\EQUIPO\Documents\Act1_AnalisisEspacial\Casos\cali\Comunas.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 1053868 ymin: 860190.2 xmax: 1068492 ymax: 879441.5
## Projected CRS: MAGNA_Colombia_Cali
comunas
## Simple feature collection with 22 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 1053868 ymin: 860190.2 xmax: 1068492 ymax: 879441.5
## Projected CRS: MAGNA_Colombia_Cali
## First 10 features:
##    OBJECTID gid comuna    nombre                       geometry
## 1         1 107      2  Comuna 2 POLYGON ((1059648 874236.3,...
## 2         2 108      1  Comuna 1 POLYGON ((1054094 875172.8,...
## 3         3 109      3  Comuna 3 POLYGON ((1061757 874615.5,...
## 4         4 110     19 Comuna 19 POLYGON ((1057817 873269.4,...
## 5         5 103     15 Comuna 15 POLYGON ((1065485 869864.1,...
## 6         6 104     17 Comuna 17 POLYGON ((1061675 868320.9,...
## 7         7 105     18 Comuna 18 POLYGON ((1059061 867223.6,...
## 8         8 106     22 Comuna 22 POLYGON ((1059486 864976.4,...
## 9         9  89      6  Comuna 6 POLYGON ((1065143 879409.5,...
## 10       10  90      4  Comuna 4 POLYGON ((1063833 877507.1,...
plot(comunas)

2. Agregacion de la base de datos de viajes

library(readxl)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
ruta_excel <- "C:/Users/EQUIPO/Documents/Act1_AnalisisEspacial/Casos/EncuestaOrigenDestino.xlsx"
datos_viajes <- read_excel(ruta_excel, sheet = 1)
## Warning: Coercing numeric to date in A2058 / R2058C1
## Warning: Coercing numeric to date in F2058 / R2058C6
## Warning: Coercing numeric to date in A2059 / R2059C1
## Warning: Coercing numeric to date in F2059 / R2059C6
## Warning: Coercing numeric to date in A2060 / R2060C1
## Warning: Coercing numeric to date in F2060 / R2060C6
## Warning: Coercing numeric to date in A2061 / R2061C1
## Warning: Coercing numeric to date in F2061 / R2061C6
## Warning: Coercing numeric to date in A2062 / R2062C1
## Warning: Coercing numeric to date in F2062 / R2062C6
## Warning: Coercing numeric to date in A2063 / R2063C1
## Warning: Coercing numeric to date in F2063 / R2063C6
## Warning: Coercing numeric to date in A2064 / R2064C1
## Warning: Coercing numeric to date in F2064 / R2064C6
## Warning: Coercing numeric to date in A2065 / R2065C1
## Warning: Coercing numeric to date in F2065 / R2065C6
## Warning: Coercing numeric to date in A2066 / R2066C1
## Warning: Coercing numeric to date in F2066 / R2066C6
## Warning: Coercing numeric to date in A2067 / R2067C1
## Warning: Coercing numeric to date in F2067 / R2067C6
## Warning: Coercing numeric to date in A2068 / R2068C1
## Warning: Coercing numeric to date in F2068 / R2068C6
## Warning: Coercing numeric to date in A2069 / R2069C1
## Warning: Coercing numeric to date in F2069 / R2069C6
## Warning: Coercing numeric to date in A2070 / R2070C1
## Warning: Coercing numeric to date in F2070 / R2070C6
## Warning: Coercing numeric to date in A2071 / R2071C1
## Warning: Coercing numeric to date in F2071 / R2071C6
## Warning: Coercing numeric to date in A2072 / R2072C1
## Warning: Coercing numeric to date in F2072 / R2072C6
## Warning: Coercing numeric to date in A2073 / R2073C1
## Warning: Coercing numeric to date in F2073 / R2073C6
## Warning: Coercing numeric to date in A2074 / R2074C1
## Warning: Coercing numeric to date in F2074 / R2074C6
## Warning: Coercing numeric to date in A2075 / R2075C1
## Warning: Coercing numeric to date in F2075 / R2075C6
## Warning: Coercing numeric to date in A2076 / R2076C1
## Warning: Coercing numeric to date in F2076 / R2076C6
## Warning: Coercing numeric to date in A2077 / R2077C1
## Warning: Coercing numeric to date in F2077 / R2077C6
## Warning: Coercing numeric to date in A2078 / R2078C1
## Warning: Coercing numeric to date in F2078 / R2078C6
## Warning: Coercing numeric to date in A2079 / R2079C1
## Warning: Coercing numeric to date in F2079 / R2079C6
## Warning: Coercing numeric to date in A2080 / R2080C1
## Warning: Coercing numeric to date in F2080 / R2080C6
## Warning: Coercing numeric to date in A2081 / R2081C1
## Warning: Coercing numeric to date in F2081 / R2081C6
## Warning: Coercing numeric to date in A2082 / R2082C1
## Warning: Coercing numeric to date in F2082 / R2082C6
## Warning: Coercing numeric to date in A2083 / R2083C1
## Warning: Coercing numeric to date in F2083 / R2083C6
## Warning: Coercing numeric to date in A2084 / R2084C1
## Warning: Coercing numeric to date in F2084 / R2084C6
## Warning: Coercing numeric to date in A2085 / R2085C1
## Warning: Coercing numeric to date in F2085 / R2085C6
## Warning: Coercing numeric to date in A2086 / R2086C1
## Warning: Coercing numeric to date in F2086 / R2086C6
## Warning: Coercing numeric to date in A2087 / R2087C1
## Warning: Coercing numeric to date in F2087 / R2087C6
## Warning: Coercing numeric to date in A2088 / R2088C1
## Warning: Coercing numeric to date in F2088 / R2088C6
## Warning: Coercing numeric to date in A2089 / R2089C1
## Warning: Coercing numeric to date in F2089 / R2089C6
## Warning: Coercing numeric to date in A2090 / R2090C1
## Warning: Coercing numeric to date in F2090 / R2090C6
## Warning: Coercing numeric to date in A2091 / R2091C1
## Warning: Coercing numeric to date in F2091 / R2091C6
## Warning: Coercing numeric to date in A2092 / R2092C1
## Warning: Coercing numeric to date in F2092 / R2092C6
## Warning: Coercing numeric to date in A2093 / R2093C1
## Warning: Coercing numeric to date in F2093 / R2093C6
## Warning: Coercing numeric to date in A2094 / R2094C1
## Warning: Coercing numeric to date in F2094 / R2094C6
## Warning: Coercing numeric to date in A2095 / R2095C1
## Warning: Coercing numeric to date in F2095 / R2095C6
## Warning: Coercing numeric to date in A2096 / R2096C1
## Warning: Coercing numeric to date in F2096 / R2096C6
## Warning: Coercing numeric to date in A2097 / R2097C1
## Warning: Coercing numeric to date in F2097 / R2097C6
## Warning: Coercing numeric to date in A2098 / R2098C1
## Warning: Coercing numeric to date in F2098 / R2098C6
## Warning: Coercing numeric to date in A2099 / R2099C1
## Warning: Coercing numeric to date in F2099 / R2099C6
## Warning: Coercing numeric to date in A2100 / R2100C1
## Warning: Coercing numeric to date in F2100 / R2100C6
## Warning: Coercing numeric to date in A2101 / R2101C1
## Warning: Coercing numeric to date in F2101 / R2101C6
## Warning: Coercing numeric to date in A2102 / R2102C1
## Warning: Coercing numeric to date in F2102 / R2102C6
## Warning: Coercing numeric to date in A2103 / R2103C1
## Warning: Coercing numeric to date in F2103 / R2103C6
## Warning: Coercing numeric to date in A2104 / R2104C1
## Warning: Coercing numeric to date in F2104 / R2104C6
## Warning: Coercing numeric to date in A2105 / R2105C1
## Warning: Coercing numeric to date in F2105 / R2105C6
## Warning: Coercing numeric to date in A2106 / R2106C1
## Warning: Coercing numeric to date in F2106 / R2106C6
## Warning: Coercing numeric to date in A2107 / R2107C1
## Warning: Coercing numeric to date in F2107 / R2107C6
## Warning: Coercing numeric to date in A2108 / R2108C1
## Warning: Coercing numeric to date in F2108 / R2108C6
## Warning: Coercing numeric to date in A2109 / R2109C1
## Warning: Coercing numeric to date in F2109 / R2109C6
## Warning: Coercing numeric to date in A2110 / R2110C1
## Warning: Coercing numeric to date in F2110 / R2110C6
## Warning: Coercing numeric to date in A2111 / R2111C1
## Warning: Coercing numeric to date in F2111 / R2111C6
## Warning: Coercing numeric to date in A2112 / R2112C1
## Warning: Coercing numeric to date in F2112 / R2112C6
## Warning: Coercing numeric to date in A2113 / R2113C1
## Warning: Coercing numeric to date in F2113 / R2113C6
## Warning: Coercing numeric to date in A2114 / R2114C1
## Warning: Coercing numeric to date in F2114 / R2114C6
## Warning: Coercing numeric to date in A2115 / R2115C1
## Warning: Coercing numeric to date in F2115 / R2115C6
## Warning: Coercing numeric to date in A2116 / R2116C1
## Warning: Coercing numeric to date in F2116 / R2116C6
## Warning: Coercing numeric to date in A2117 / R2117C1
## Warning: Coercing numeric to date in F2117 / R2117C6
## Warning: Coercing numeric to date in A2118 / R2118C1
## Warning: Coercing numeric to date in F2118 / R2118C6
## Warning: Coercing numeric to date in A2119 / R2119C1
## Warning: Coercing numeric to date in F2119 / R2119C6
## Warning: Coercing numeric to date in A2120 / R2120C1
## Warning: Coercing numeric to date in F2120 / R2120C6
## Warning: Coercing numeric to date in A2121 / R2121C1
## Warning: Coercing numeric to date in F2121 / R2121C6
## Warning: Coercing numeric to date in A2122 / R2122C1
## Warning: Coercing numeric to date in F2122 / R2122C6
## Warning: Coercing numeric to date in A2123 / R2123C1
## Warning: Coercing numeric to date in F2123 / R2123C6
## Warning: Coercing numeric to date in A2124 / R2124C1
## Warning: Coercing numeric to date in F2124 / R2124C6
## Warning: Coercing numeric to date in A2125 / R2125C1
## Warning: Coercing numeric to date in F2125 / R2125C6
## Warning: Coercing numeric to date in A2126 / R2126C1
## Warning: Coercing numeric to date in F2126 / R2126C6
## Warning: Coercing numeric to date in A2127 / R2127C1
## Warning: Coercing numeric to date in F2127 / R2127C6
## Warning: Coercing numeric to date in A2128 / R2128C1
## Warning: Coercing numeric to date in F2128 / R2128C6
## Warning: Coercing numeric to date in A2129 / R2129C1
## Warning: Coercing numeric to date in F2129 / R2129C6
## Warning: Coercing numeric to date in A2130 / R2130C1
## Warning: Coercing numeric to date in F2130 / R2130C6
## Warning: Coercing numeric to date in A2131 / R2131C1
## Warning: Coercing numeric to date in F2131 / R2131C6
## Warning: Coercing numeric to date in A2132 / R2132C1
## Warning: Coercing numeric to date in F2132 / R2132C6
## Warning: Coercing numeric to date in A2133 / R2133C1
## Warning: Coercing numeric to date in F2133 / R2133C6
## Warning: Coercing numeric to date in A2134 / R2134C1
## Warning: Coercing numeric to date in F2134 / R2134C6
## Warning: Coercing numeric to date in A2135 / R2135C1
## Warning: Coercing numeric to date in F2135 / R2135C6
## Warning: Coercing numeric to date in A2136 / R2136C1
## Warning: Coercing numeric to date in F2136 / R2136C6
## Warning: Coercing numeric to date in A2137 / R2137C1
## Warning: Coercing numeric to date in F2137 / R2137C6
## Warning: Coercing numeric to date in A2138 / R2138C1
## Warning: Coercing numeric to date in F2138 / R2138C6
## Warning: Coercing text to numeric in J2333 / R2333C10: '1'
## Warning: Coercing text to numeric in J2334 / R2334C10: '6'
## Warning: Coercing text to numeric in J2335 / R2335C10: '11'
## Warning: Coercing text to numeric in J2336 / R2336C10: '6'
## Warning: Coercing text to numeric in J2337 / R2337C10: '6'
## Warning: Coercing text to numeric in J2338 / R2338C10: '6'
## Warning: Coercing text to numeric in J2339 / R2339C10: '6'
## Warning: Expecting logical in X3860 / R3860C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X4164 / R4164C24: got 'GRUA'
## Warning: Expecting logical in Q6623 / R6623C17: got 'BICICLETA'
## Warning: Expecting logical in X6758 / R6758C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X6900 / R6900C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X7914 / R7914C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X11168 / R11168C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X11833 / R11833C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X12398 / R12398C24: got 'VAN ESCOLAR'
## Warning: Expecting logical in Q13865 / R13865C17: got 'BUSETA'
## Warning: Expecting logical in X17913 / R17913C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X19592 / R19592C24: got 'MOTO-CARRO'
## Warning: Expecting logical in Q22244 / R22244C17: got 'BICICLETA'
## Warning: Expecting logical in X22329 / R22329C24: got 'BUS ESCOLAR'
## Warning: Expecting logical in X24019 / R24019C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X25200 / R25200C24: got 'CARRETILLA'
## Warning: Coercing text to numeric in N26235 / R26235C14: '3'
## Warning: Coercing text to numeric in J26503 / R26503C10: '6'
## Warning: Coercing text to numeric in J26935 / R26935C10: '7'
## Warning: Coercing text to numeric in J26936 / R26936C10: '6'
## Warning: Coercing text to numeric in J26937 / R26937C10: '6'
## Warning: Coercing text to numeric in J26938 / R26938C10: '1'
## Warning: Coercing text to numeric in J26939 / R26939C10: '1'
## Warning: Coercing text to numeric in J26940 / R26940C10: '6'
## Warning: Coercing text to numeric in J26941 / R26941C10: '1'
## Warning: Coercing text to numeric in J26942 / R26942C10: '6'
## Warning: Coercing text to numeric in J26943 / R26943C10: '6'
## Warning: Coercing text to numeric in J26944 / R26944C10: '1'
## Warning: Coercing text to numeric in J26945 / R26945C10: '1'
## Warning: Coercing text to numeric in J26946 / R26946C10: '1'
## Warning: Coercing text to numeric in J26947 / R26947C10: '6'
## Warning: Coercing text to numeric in J26948 / R26948C10: '8'
## Warning: Coercing text to numeric in J26949 / R26949C10: '6'
## Warning: Coercing text to numeric in J26950 / R26950C10: '1'
## Warning: Coercing text to numeric in J26951 / R26951C10: '7'
## Warning: Coercing text to numeric in J26952 / R26952C10: '6'
## Warning: Coercing text to numeric in J26953 / R26953C10: '6'
## Warning: Coercing text to numeric in J26954 / R26954C10: '6'
## Warning: Coercing text to numeric in J26955 / R26955C10: '6'
## Warning: Coercing text to numeric in J26956 / R26956C10: '6'
## Warning: Coercing text to numeric in J26957 / R26957C10: '6'
## Warning: Coercing text to numeric in J26958 / R26958C10: '6'
## Warning: Coercing text to numeric in J26959 / R26959C10: '6'
## Warning: Coercing text to numeric in J26960 / R26960C10: '6'
## Warning: Coercing text to numeric in J26961 / R26961C10: '6'
## Warning: Coercing text to numeric in J26962 / R26962C10: '6'
## Warning: Coercing text to numeric in J26963 / R26963C10: '6'
## Warning: Coercing text to numeric in J26964 / R26964C10: '6'
## Warning: Coercing text to numeric in J26965 / R26965C10: '1'
## Warning: Coercing text to numeric in J26966 / R26966C10: '1'
## Warning: Coercing text to numeric in J26967 / R26967C10: '1'
## Warning: Coercing text to numeric in J26968 / R26968C10: '1'
## Warning: Coercing text to numeric in J26969 / R26969C10: '1'
## Warning: Coercing text to numeric in J26970 / R26970C10: '1'
## Warning: Coercing text to numeric in J26971 / R26971C10: '6'
## Warning: Coercing text to numeric in J26972 / R26972C10: '6'
## Warning: Coercing text to numeric in J26973 / R26973C10: '6'
## Warning: Coercing text to numeric in J26974 / R26974C10: '6'
## Warning: Coercing text to numeric in J26975 / R26975C10: '1'
## Warning: Coercing text to numeric in J26976 / R26976C10: '1'
## Warning: Coercing text to numeric in J26977 / R26977C10: '1'
## Warning: Coercing text to numeric in J26978 / R26978C10: '6'
## Warning: Coercing text to numeric in J26979 / R26979C10: '6'
## Warning: Coercing text to numeric in J26980 / R26980C10: '6'
## Warning: Coercing text to numeric in J26981 / R26981C10: '6'
## Warning: Coercing text to numeric in J26982 / R26982C10: '6'
## Warning: Coercing text to numeric in J26983 / R26983C10: '1'
## Warning: Coercing text to numeric in J26984 / R26984C10: '1'
## Warning: Coercing text to numeric in J26985 / R26985C10: '1'
## Warning: Coercing text to numeric in J26986 / R26986C10: '6'
## Warning: Coercing text to numeric in J26987 / R26987C10: '6'
## Warning: Coercing text to numeric in J26988 / R26988C10: '6'
## Warning: Coercing text to numeric in J26989 / R26989C10: '6'
## Warning: Coercing text to numeric in J26990 / R26990C10: '1'
## Warning: Coercing text to numeric in J26991 / R26991C10: '6'
## Warning: Coercing text to numeric in J26992 / R26992C10: '1'
## Warning: Coercing text to numeric in J26993 / R26993C10: '6'
## Warning: Coercing text to numeric in J26994 / R26994C10: '6'
## Warning: Coercing text to numeric in J26995 / R26995C10: '6'
## Warning: Coercing text to numeric in J26996 / R26996C10: '6'
## Warning: Coercing text to numeric in J26997 / R26997C10: '11'
## Warning: Coercing text to numeric in J26998 / R26998C10: '6'
## Warning: Coercing text to numeric in J26999 / R26999C10: '1'
## Warning: Coercing text to numeric in J27000 / R27000C10: '6'
## Warning: Coercing text to numeric in J27001 / R27001C10: '1'
## Warning: Coercing text to numeric in J27002 / R27002C10: '6'
## Warning: Coercing text to numeric in J27003 / R27003C10: '11'
## Warning: Coercing text to numeric in J27004 / R27004C10: '6'
## Warning: Coercing text to numeric in J27005 / R27005C10: '6'
## Warning: Coercing text to numeric in J27006 / R27006C10: '6'
## Warning: Coercing text to numeric in J27007 / R27007C10: '6'
## Warning: Coercing text to numeric in J27008 / R27008C10: '6'
## Warning: Coercing text to numeric in J27009 / R27009C10: '2'
## Warning: Coercing text to numeric in J27010 / R27010C10: '6'
## Warning: Coercing text to numeric in J27011 / R27011C10: '6'
## Warning: Coercing text to numeric in J27012 / R27012C10: '11'
## Warning: Coercing text to numeric in J27013 / R27013C10: '6'
## Warning: Coercing text to numeric in J27014 / R27014C10: '1'
## Warning: Coercing text to numeric in J27015 / R27015C10: '6'
## Warning: Coercing text to numeric in J27016 / R27016C10: '6'
## Warning: Coercing text to numeric in J27017 / R27017C10: '6'
## Warning: Coercing text to numeric in J27018 / R27018C10: '6'
## Warning: Coercing text to numeric in J27019 / R27019C10: '6'
## Warning: Coercing text to numeric in J27020 / R27020C10: '6'
## Warning: Coercing text to numeric in J27021 / R27021C10: '11'
## Warning: Coercing text to numeric in J27022 / R27022C10: '6'
## Warning: Coercing text to numeric in J27023 / R27023C10: '4'
## Warning: Coercing text to numeric in J27024 / R27024C10: '6'
## Warning: Coercing text to numeric in J27025 / R27025C10: '2'
## Warning: Coercing text to numeric in J27026 / R27026C10: '2'
## Warning: Coercing text to numeric in J27027 / R27027C10: '11'
## Warning: Coercing text to numeric in J27028 / R27028C10: '2'
## Warning: Coercing text to numeric in J27029 / R27029C10: '2'
## Warning: Coercing text to numeric in J27030 / R27030C10: '2'
## Warning: Expecting logical in X31653 / R31653C24: got 'VOLQUETA'
## Warning: Expecting numeric in J31842 / R31842C10: got 'ANULADO'
## Warning: Coercing numeric to date in A33837 / R33837C1
## Warning: Coercing numeric to date in F33837 / R33837C6
## Warning: Expecting logical in X34736 / R34736C24: got 'MOTO-CARRO'
## Warning: Expecting logical in X35053 / R35053C24: got 'MOTO-CARRO'
## New names:
## • `MUNICIPIO` -> `MUNICIPIO...7`
## • `DEPARTAMENTO / LOCALIDAD / COMUNA / DISTRITO / BARRIO / VEREDA / HITO /
##   DIRECCIÓN` -> `DEPARTAMENTO / LOCALIDAD / COMUNA / DISTRITO / BARRIO / VEREDA
##   / HITO / DIRECCIÓN...8`
## • `MUNICIPIO` -> `MUNICIPIO...11`
## • `DEPARTAMENTO / LOCALIDAD / COMUNA / DISTRITO / BARRIO / VEREDA / HITO /
##   DIRECCIÓN` -> `DEPARTAMENTO / LOCALIDAD / COMUNA / DISTRITO / BARRIO / VEREDA
##   / HITO / DIRECCIÓN...12`
## • `OTRO ¿CUÁL?` -> `OTRO ¿CUÁL?...17`
## • `OTRO ¿CUÁL?` -> `OTRO ¿CUÁL?...24`
# Agregamos los datos por comuna de origen
conteo_origen <- datos_viajes %>%
  # se excluyen los orígenes fuera de la ciudad y valores vacíos
  filter(`comuna origen` != "Fuera de Cali" & !is.na(`comuna origen`)) %>%
  # Agrupamos por la columna de interés
  group_by(`comuna origen`) %>%
  # se hace un conteo de la cantidad de registros (viajes) por comuna
  summarise(total_origenes = n()) %>%
  # Convertir a texto
  mutate(id_comuna = as.numeric(`comuna origen`))

head(conteo_origen)
## # A tibble: 6 × 3
##   `comuna origen` total_origenes id_comuna
##   <chr>                    <int>     <dbl>
## 1 0                          672         0
## 2 01                         794         1
## 3 02                        2968         2
## 4 03                        2121         3
## 5 04                        1493         4
## 6 05                         664         5

3. Realizar el Join para integrar el shapefile con una nueva columna de la cantidad de origenes de viajes por comuna

mapa_viajes <- comunas %>%
  left_join(conteo_origen, by = c("comuna" = "id_comuna")) %>%
  st_transform(4326)

En este caso, pineso usar leaftlet como renderizador del mapa, por eso hago la tranformacion de sistema de coordenadas de MAGNA a WGS84, que es con el que funciona leaftlet

4. Creacion del mapa de cantidad de viajes con origen por comuna

library(leaflet)
paleta <- colorNumeric("YlOrRd", domain = mapa_viajes$total_origenes, na.color = "transparent")

leaflet(mapa_viajes) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~paleta(total_origenes),
    weight = 1,
    color = "white",
    fillOpacity = 0.8,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("Comuna: ", comuna, " | Viajes origen: ", total_origenes)
  ) %>%
  addLegend(
    pal = paleta, 
    values = ~total_origenes, 
    title = "Total Viajes (Origen)",
    position = "bottomright"
  )

Del mapa podemos visualizar que las comunas 2 y 19 son las que presentan mas de 2500 viajes cuyo origen se encuentra en ellas, seguido de la comuna 17 con 2377 y la comuna 3 con 2121

4.1. Mapa de origenes por tipo de vehiculo 1: Bicicleta

viajes_bici <- datos_viajes %>%
  filter(`comuna origen` != "Fuera de Cali" & !is.na(`comuna origen`)) %>%
  filter(`TIPO DE VEHÍCULO` == 1) %>%
  group_by(`comuna origen`) %>%
  summarise(total_viajes = n(), .groups = "drop") %>%
  mutate(id_comuna = as.numeric(`comuna origen`))

mapa_bici <- comunas %>%
  left_join(viajes_bici, by = c("comuna" = "id_comuna")) %>%
  st_transform(4326)

# Reemplazo de valores nulos (NA) con 0 en comunas donde no hubo viajes registrados
mapa_bici$total_viajes[is.na(mapa_bici$total_viajes)] <- 0

paleta_bici <- colorNumeric("Greens", domain = mapa_bici$total_viajes, na.color = "transparent")

leaflet(mapa_bici) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~paleta_bici(total_viajes),
    weight = 1,
    color = "white",
    fillOpacity = 0.8,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("Comuna: ", comuna, " | Bicicletas: ", total_viajes)
  ) %>%
  addLegend(
    pal = paleta_bici, 
    values = ~total_viajes, 
    title = "Viajes en Bicicleta",
    position = "bottomright"
  )

El mapa nos muestra que las comunas 2, 19 y 17 son las que puntean con mas personas que empiezan sus viajes en bicicleta, seguido de las comunas 18 y 3

4.2. Mapa de origenes por tipo de vehiculo 2: Moto

viajes_moto <- datos_viajes %>%
  filter(`comuna origen` != "Fuera de Cali" & !is.na(`comuna origen`)) %>%
  filter(`TIPO DE VEHÍCULO` == 2) %>%
  group_by(`comuna origen`) %>%
  summarise(total_viajes = n(), .groups = "drop") %>%
  mutate(id_comuna = as.numeric(`comuna origen`))

mapa_moto <- comunas %>%
  left_join(viajes_moto, by = c("comuna" = "id_comuna")) %>%
  st_transform(4326)

mapa_moto$total_viajes[is.na(mapa_moto$total_viajes)] <- 0

paleta_moto <- colorNumeric("Reds", domain = mapa_moto$total_viajes, na.color = "transparent")

leaflet(mapa_moto) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~paleta_moto(total_viajes),
    weight = 1,
    color = "white",
    fillOpacity = 0.8,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("Comuna: ", comuna, " | Motos: ", total_viajes)
  ) %>%
  addLegend(
    pal = paleta_moto, 
    values = ~total_viajes, 
    title = "Viajes en Moto",
    position = "bottomright"
  )

El mapa relacionado con los origenes de viajes en moto, muestra el mismo patron de distribucion espacial que en bicicleta

4.3. Mapa de origenes por tipo de vehiculo 3: Automovil

viajes_auto <- datos_viajes %>%
  filter(`comuna origen` != "Fuera de Cali" & !is.na(`comuna origen`)) %>%
  filter(`TIPO DE VEHÍCULO` == 3) %>%
  group_by(`comuna origen`) %>%
  summarise(total_viajes = n(), .groups = "drop") %>%
  mutate(id_comuna = as.numeric(`comuna origen`))

mapa_auto <- comunas %>%
  left_join(viajes_auto, by = c("comuna" = "id_comuna")) %>%
  st_transform(4326)

mapa_auto$total_viajes[is.na(mapa_auto$total_viajes)] <- 0

paleta_auto <- colorNumeric("Blues", domain = mapa_auto$total_viajes, na.color = "transparent")

leaflet(mapa_auto) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~paleta_auto(total_viajes),
    weight = 1,
    color = "white",
    fillOpacity = 0.8,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("Comuna: ", comuna, " | Automóviles: ", total_viajes)
  ) %>%
  addLegend(
    pal = paleta_auto, 
    values = ~total_viajes, 
    title = "Viajes en Automóvil",
    position = "bottomright"
  )

El mapa relacionado con los origenes de viajes en automovil, muestra el mismo patron de distribucion espacial que en bicicleta y moto, siendo concordantes con el mapa de total de origenes

5. Creacion del mapa de cantidad de viajes con destino por comuna

crear_mapa_destino <- function(datos, shape, tipo_num = NULL, nombre_mapa, paleta_color) {
  
  datos_filtrados <- datos %>%
    filter(`comuna destino` != "Fuera de Cali" & !is.na(`comuna destino`))
  
  if (!is.null(tipo_num)) {
    datos_filtrados <- datos_filtrados %>% filter(`TIPO DE VEHÍCULO` == tipo_num)
  }
  
  conteo <- datos_filtrados %>%
    group_by(`comuna destino`) %>%
    summarise(total_viajes = n(), .groups = "drop") %>%
    mutate(id_comuna = as.numeric(`comuna destino`))
  
  mapa_datos <- shape %>%
    left_join(conteo, by = c("comuna" = "id_comuna")) %>%
    st_transform(4326)
  
  mapa_datos$total_viajes[is.na(mapa_datos$total_viajes)] <- 0
  
  paleta <- colorNumeric(paleta_color, domain = mapa_datos$total_viajes, na.color = "transparent")
  
  leaflet(mapa_datos) %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
    addPolygons(
      fillColor = ~paleta(total_viajes),
      weight = 1, color = "white", fillOpacity = 0.8,
      highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
      label = ~paste0("Comuna: ", comuna, " | ", nombre_mapa, ": ", total_viajes)
    ) %>%
    addLegend(pal = paleta, values = ~total_viajes, title = nombre_mapa, position = "bottomright")
}

mapa_total_dest <- crear_mapa_destino(datos_viajes, comunas, NULL, "Total Destinos", "YlOrRd")

mapa_total_dest

El mapa nos muestra que la mayor cantidad de viajes terminan en la comuna 2 con mas de 4500 registros, seguido de la cmuna 3 y la comuna 19, aqui tambien las comunas del sur toman un poco mas de protagonismo con relacion a los origenes

5.1. Mapa de destinos por tipo de vehiculo 1: Bicicleta

mapa_bici_dest <- crear_mapa_destino(datos_viajes, comunas, 1, "Destinos Bicicleta", "Greens")

mapa_bici_dest

Las distribuciones espaciales de los destinos de los viajes en bicicleta conservan la misma distribucion que el mapa de total de destinos

5.2. Mapa de destinos por tipo de vehiculo 2: Moto

mapa_moto_dest <- crear_mapa_destino(datos_viajes, comunas, 2, "Destinos Moto", "Reds")

mapa_moto_dest

Las distribuciones espaciales de los destinos de los viajes en moto conservan la misma distribucion que el mapa de total de destinos

5.3. Mapa de destinos por tipo de vehiculo 3: Automovil

mapa_auto_dest <- crear_mapa_destino(datos_viajes, comunas, 3, "Destinos Automóvil", "Blues")

mapa_auto_dest

Las distribuciones espaciales de los destinos de los viajes en auto conservan la misma distribucion que el mapa de total de destinos

6. Analisis de resultados

Las comunas con mayor cantidad de viajes originados suelen concentrarse en áreas residenciales consolidadas o de estratos medios-altos, tendientes al area norte de la ciudad, donde la capacidad de movilización es mayor. Estas zonas demandan una infraestructura robusta para encauzar el flujo hacia las vías arterias.

En cambio, los viajes terminan predominantemente en el centro ampliado, zonas industriales (norte/Yumbo) o clústeres educativos y comerciales (sur). Esta concentración de destinos genera cuellos de botella en los corredores principales durante las horas pico.

Se evidencia una clara concentración de destinos en el centro y norte de la ciudad (comunas con tonos rojos más oscuros), áreas tradicionalmente asociadas a la actividad económica, industrial y de servicios. Esto, contrastado con orígenes más dispersos en oriente y sur, confirma el patrón de viajes pendulares (periferia-centro).

7. Conclusiones y posibles desiciones a tomar