prueba

library("readr")
library("DT")
library("ggplot2")
library("dplyr")
library("VIM")
library("naniar")
library("minqa")
library("mice")
library("tidyverse")
library("hrbrthemes")
library("gridExtra")
library("missForest")
library("outliers")
library("EnvStats")
library("sf")
library("leaflet")
library("stringr")
d1<-read_csv("C:/Users/valcd/Downloads/precios_t12023.csv")
Rows: 66700 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): BANDERA, NOMBRE COMERCIAL, PRODUCTO, FECHA REGISTRO, DEPARTAMENTO, ...
dbl (1): VALOR PRECIO

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d2<-read_csv("C:/Users/valcd/Downloads/precios_t22023.csv")
Rows: 74112 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): BANDERA, NOMBRE COMERCIAL, PRODUCTO, FECHA REGISTRO, DEPARTAMENTO, ...
dbl (1): VALOR PRECIO

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d3<-read_csv("C:/Users/valcd/Downloads/precios_t32023.csv")
Rows: 62339 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): BANDERA, NOMBRE COMERCIAL, PRODUCTO, FECHA REGISTRO, DEPARTAMENTO, ...
dbl (1): VALOR PRECIO

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d4<-read_csv("C:/Users/valcd/Downloads/precios_t42023.csv")
Rows: 64792 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): BANDERA, NOMBRE COMERCIAL, PRODUCTO, FECHA REGISTRO, DEPARTAMENTO, ...
dbl (1): VALOR PRECIO

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data23 <- bind_rows(d1,d2,d3,d4)
data23_1 <- data23 %>% filter(data23$PRODUCTO == "GASOLINA MOTOR")
library("ggmap")
ℹ Google's Terms of Service: <https://mapsplatform.google.com>
  Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
  OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
api_key <- 'AIzaSyC8RXGL3mkNliEqmq_rWVwkyYt9sba34I0'
register_google(key = api_key)

get_googlemap("Colombia",
              maptype = "hybrid",
              scale = 2,
              zoom = 7) %>% ggmap()
ℹ <https://maps.googleapis.com/maps/api/staticmap?center=Colombia&zoom=7&size=640x640&scale=2&maptype=hybrid&key=xxx>
ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=Colombia&key=xxx>

mapa_col <- st_read("C:/Users/valcd/Downloads/Python _Act/Python _Act/coordenadas/COLOMBIA/COLOMBIA.shp", quiet = FALSE)
Reading layer `COLOMBIA' from data source 
  `C:\Users\valcd\Downloads\Python _Act\Python _Act\coordenadas\COLOMBIA\COLOMBIA.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 33 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.73575 ymin: -4.227907 xmax: -66.84735 ymax: 13.39453
Geodetic CRS:  WGS 84
mapa_col <- st_make_valid(mapa_col)
library("stringr")

dpto_1 <- tolower(data23_1$DEPARTAMENTO)  
dpto_2 <- tolower(mapa_col$DPTO_CNMBR)  

M1 <- which(is.na(match(dpto_1, dpto_2)))



dpto_2 <- str_replace_all(dpto_2, "\\?", "ñ")
dpto_2 <- str_replace_all(dpto_2, "archipielago de san andres", "san andres islas")


data23_1$codigo <- dpto_1
mapa_col$codigo <- dpto_2

data_23_2 <- merge(st_drop_geometry(mapa_col), data23_1, by = "codigo", all = TRUE, sort = FALSE)

data_23_3 <- data_23_2  %>%
  group_by(codigo, DPTO_NANO_, DPTO_NAREA, DPTO_CSMBL, DPTO_NANO, PAIS_PAIS_, SHAPE_Leng, SHAPE_Area) %>%
  summarize(precio_prom_dpto = mean(`VALOR PRECIO`)) %>%
  ungroup()
`summarise()` has grouped output by 'codigo', 'DPTO_NANO_', 'DPTO_NAREA',
'DPTO_CSMBL', 'DPTO_NANO', 'PAIS_PAIS_', 'SHAPE_Leng'. You can override using
the `.groups` argument.
data_23_3$precio_prom_dpto <- as.numeric(data_23_3$precio_prom_dpto)
precio_fac <- cut(data_23_3$precio_prom_dpto, 
                  breaks = c(10000, 12000, 14000, 16000),
                  labels = c("Bajo", "Medio", "Alto"))
colores <- c("Bajo" = "green",
             "Medio" = "orange",
             "Alto" = "red")

if (length(precio_fac) == nrow(data_23_3)) {
  data_23_3$color <- colores[precio_fac]
  cat("Colores asignados correctamente.\n")
} else {
  stop("Error: La longitud de precio_fac no coincide con el número de filas en datos_unidos2.")
}
Colores asignados correctamente.
data_23_4 <- merge(mapa_col, data_23_3, by = "codigo")
library(mapview)

mapview(data_23_4, zcol = "precio_prom_dpto", col.regions = c("green", "orange", "red"))
Warning: Found less unique colors (3) than unique zcol values (32)! 
Interpolating color vector to match number of zcol values.