Paso 1: Carga de las librerías necesarias

library(readxl)
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(sf)
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(ggplot2)
library(viridis)
## Cargando paquete requerido: viridisLite

Paso 2: Cargar los datos y el mapa espacial

datos_eod <- read_excel("C:/Maestria/Geografia/Actividad_1/Casos/EncuestaOrigenDestino.xlsx", sheet = "Hoja1") 
## 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`
mapa_comunas <- st_read("C:/Maestria/Geografia/Actividad_1/Casos/cali/Comunas.shp", quiet = TRUE)

Paso 3: Limpiar y filtrar los datos (Solo ciudad de Cali)

datos_cali <- datos_eod %>%
  # Mantener solo orígenes y destinos dentro de Cali válidos
  filter(!(`comuna origen` %in% c("Fuera de Cali", "0", 0)),
         !(`comuna destino` %in% c("Fuera de Cali", "0", 0))) %>%
  # Convertir a formato numérico ('02' pasará a ser 2)
  mutate(
    comuna_origen  = as.numeric(`comuna origen`),
    comuna_destino = as.numeric(`comuna destino`),
    vehiculo       = as.numeric(`TIPO DE VEHÍCULO`)
  )

Paso 4: Calcular los conteos para los 8 mapas

# --- MAPAS DE ORIGEN (De dónde sale la gente) ---
# Mapa 1: Origen Total
orig_total <- datos_cali %>% count(comuna = comuna_origen, name = "viajes")

# Mapas 2, 3 y 4: Origen por tipo de vehículo
orig_bici <- datos_cali %>% filter(vehiculo == 1) %>% count(comuna = comuna_origen, name = "viajes")
orig_moto <- datos_cali %>% filter(vehiculo == 2) %>% count(comuna = comuna_origen, name = "viajes")
orig_auto <- datos_cali %>% filter(vehiculo == 3) %>% count(comuna = comuna_origen, name = "viajes")

# --- MAPAS DE DESTINO (Hacia dónde va la gente) ---
# Mapa 5: Destino Total
dest_total <- datos_cali %>% count(comuna = comuna_destino, name = "viajes")

# Mapas 6, 7 y 8: Destino por tipo de vehículo
dest_bici <- datos_cali %>% filter(vehiculo == 1) %>% count(comuna = comuna_destino, name = "viajes")
dest_moto <- datos_cali %>% filter(vehiculo == 2) %>% count(comuna = comuna_destino, name = "viajes")
dest_auto <- datos_cali %>% filter(vehiculo == 3) %>% count(comuna = comuna_destino, name = "viajes")

Paso 5: Unir los cálculos con el mapa espacial

# Unir datos de origen al shapefile
mapa_orig_total <- left_join(mapa_comunas, orig_total, by = "comuna")
mapa_orig_bici  <- left_join(mapa_comunas, orig_bici, by = "comuna")
mapa_orig_moto  <- left_join(mapa_comunas, orig_moto, by = "comuna")
mapa_orig_auto  <- left_join(mapa_comunas, orig_auto, by = "comuna")

# Unir datos de destino al shapefile
mapa_dest_total <- left_join(mapa_comunas, dest_total, by = "comuna")
mapa_dest_bici  <- left_join(mapa_comunas, dest_bici, by = "comuna")
mapa_dest_moto  <- left_join(mapa_comunas, dest_moto, by = "comuna")
mapa_dest_auto  <- left_join(mapa_comunas, dest_auto, by = "comuna")

Paso 6: Mapas

# Función maestra de diseño de mapas ajustada para mostrar el número de comuna
crear_mapa <- function(datos_espaciales, titulo) {
  ggplot(datos_espaciales) +
    # Capa de los polígonos coloreados por la cantidad de viajes
    geom_sf(aes(fill = viajes), color = "white", size = 0.3) +
    
    # NUEVA CAPA: Agrega el número de la comuna en el centro de cada polígono
    # Asegúrate de que la columna con el número se llame 'comuna' en tu base unida
    geom_sf_text(aes(label = comuna), color = "white", size = 3, fontface = "bold", 
                 check_overlap = TRUE) + # check_overlap evita que los números se amontonen
    
    # Paleta de colores visualmente intuitiva (tonos cálidos = alta densidad)
    scale_fill_viridis(option = "magma", direction = -1, na.value = "grey90", name = "Viajes") +
    theme_minimal() +
    labs(title = titulo, subtitle = "Ciudad de Cali - Encuesta Origen Destino") +
    theme(
      axis.text = element_blank(),
      axis.ticks = element_blank(),
      panel.grid = element_blank(),
      plot.title = element_text(face = "bold", size = 14)
    )
}

# Construcción de los 8 mapas
mapa_1 <- crear_mapa(mapa_orig_total, "1. Origen Total (De dónde sale la mayoría)")
mapa_2 <- crear_mapa(mapa_orig_bici, "2. Origen: Viajes en Bicicleta")
mapa_3 <- crear_mapa(mapa_orig_moto, "3. Origen: Viajes en Moto")
mapa_4 <- crear_mapa(mapa_orig_auto, "4. Origen: Viajes en Automóvil")

mapa_5 <- crear_mapa(mapa_dest_total, "5. Destino Total (Hacia dónde van)")
mapa_6 <- crear_mapa(mapa_dest_bici, "6. Destino: Viajes en Bicicleta")
mapa_7 <- crear_mapa(mapa_dest_moto, "7. Destino: Viajes en Moto")
mapa_8 <- crear_mapa(mapa_dest_auto, "8. Destino: Viajes en Automóvil")

# Imprimir los mapas para visualizarlos en RStudio
print(mapa_1)

print(mapa_2) 

print(mapa_3) 

print(mapa_4)

print(mapa_5)

print(mapa_6)

print(mapa_7)

print(mapa_8)