Домашнее задание 18

Author

Курганов Ярослав

Published

April 5, 2026

library(tidyverse)
library(leaflet)
library(sf)

#загружаем данные
load("~/Yandex.Disk.localized/libraries_stat_2023.Rdata") 
load("~/Yandex.Disk.localized/libs_sf (1).Rdata")

#оформляем необходимую иконку
icon_path <- "~/Yandex.Disk.localized/иконка дз.jpg" 
my_icon <- makeIcon(
  iconUrl = icon_path,
  iconWidth = 30, 
  iconHeight = 30,
  iconAnchorX = 15, 
  iconAnchorY = 30
)

#настраиваем цветовую палитру как в исходном примере
my_colors <- c(
  "#684687", "#64629c", "#5982a2", "#549ba3", 
  "#55ae9f", "#65c391", "#8fd776", "#d7e560"
)

my_bins <- c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, Inf)

pal <- colorBin(
  palette = my_colors, 
  domain = libraries_stat_2023$density,
  bins = my_bins,
  na.color = "transparent"
)

#строим саму карту
map <- leaflet() |>
  addTiles() |>
  
  #цветовое оформление конкретных регионов
  addPolygons(
    data = libraries_stat_2023,
    fillColor = ~pal(density),
    weight = 1,
    opacity = 1,
    color = "white",
    fillOpacity = 0.8,
    highlightOptions = highlightOptions(
      weight = 3,
      color = "#666",
      bringToFront = TRUE
    ),
    label = ~paste0(region, ": ", density)
  ) |>
  
  #оформление точек с библиотеками
  addMarkers(
    data = libs_sf,
    icon = my_icon,
    clusterOptions = markerClusterOptions(),
    popup = ~paste0("<b>", name, "</b><br>", address)
  ) |>
  
  #добавляем показатели по библиотекам с соотношением по цветам (все максимально приблизительно к исходной карте)
  addLegend(
    colors = my_colors,                
    labels = c("0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8"), 
    title = "Библиотек на 1000 чел.",
    position = "bottomright",
    opacity = 1,
    className = "info legend"       
  )

map