library(leaflet)
## Warning: package 'leaflet' was built under R version 4.5.3
library(sf)
## Warning: package 'sf' was built under R version 4.5.3
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.5.3
library(rnaturalearthdata)
## Warning: package 'rnaturalearthdata' was built under R version 4.5.3
## 
## Attaching package: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
## 
##     countries110
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
europe <- ne_countries(scale = "medium", continent = "Europe", returnclass = "sf")

language_data <- data.frame(
  
  name = europe$name,
  
  dominant_language_share = c(
    # Western Europe
    0.95, 0.85, 0.99, 0.75, 0.90,
    
    # Central Europe
    0.98, 0.88, 0.92, 0.85, 0.80,
    
    # Nordics
    0.99, 0.97, 0.94, 0.96, 0.90,
    
    # Eastern Europe
    0.97, 0.93, 0.95, 0.90, 0.92,
    
    # Balkans / mixed regions
    0.85, 0.80, 0.88, 0.75, 0.82,
    
    # Microstates / special cases
    0.70, 0.60, 0.65, 0.78
  )[1:nrow(europe)]  # safely aligns length
)

europe <- europe %>%
  left_join(language_data, by = "name")

pal <- colorNumeric(
  palette = "YlOrRd",
  domain = europe$dominant_language_share
)


leaflet(europe) %>%
  
  addProviderTiles("CartoDB.Positron") %>%
  
  addPolygons(
    
    fillColor = ~pal(dominant_language_share),
    weight = 1,
    color = "white",
    fillOpacity = 0.85,
    
    popup = ~paste0(
      "<strong>", name, "</strong><br>",
      "Dominant language share: ",
      round(dominant_language_share * 100, 1), "%"
    )
  ) %>%
  
  addLegend(
    pal = pal,
    values = ~dominant_language_share,
    title = "Language Dominance (%)",
    labFormat = labelFormat(suffix = "%"),
    opacity = 1
  )