rleafletmousehoverrnaturalearth

Mousehover over country in leaflet to see its name


I am creating a map of the world:

library(leaflet)
library(rnaturalearth)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1)

Is it possible to either hover over any country to see its name or to click on any country to see its name?

Thanks a lot!


Solution

  • Or, even simpler:

    library(leaflet)
    library(rnaturalearth)
    
    countries <- rnaturalearth::countries110
    mymap <- leaflet(countries)
    mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2, 
                          fillOpacity = 1, label = ~name)
    

    As long as the data for the labels is part of the data frame to be plotted the ~ notation works like a charm.