anyone please guide what is the purpose of library(charlatan) and ch_name(5) in under mention code and if i want to replace latitude and longitude of my locations how i can do it in visualization stuff in leaflet? this code is working as it is but i am unable to understand how i can replace lat and long of my choice in leaflet function ?
library(sf)
library(dplyr)
library(leaflet)
library(charlatan)
somewhere in pakistan...
ingroup <- data.frame(lat=df1$i.lat, lon=df1$i.lon) %>%
mutate(name = ch_name(5)) %>%
st_as_sf(coords = c("lat","lon"), crs=4326)
ingroup
somewhere else in pakistan...
outgroup <- data.frame(lat=df1$lat,lon=df1$lon) %>%
mutate(name = ch_name(5)) %>%
st_as_sf(coords = c("lat","lon"), crs=4326)
outgroup
find the nearest neighbour: sf::st_nearest_feature (computationally very efficient)
ingroup <- ingroup %>%
mutate(nearest_out = outgroup$name[st_nearest_feature(., outgroup)]) %>%
mutate(label = paste0("ingroup: ", name, "
nearest outgroup is: ", nearest_out))
And now visualize the stuff!
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = ingroup,
fillColor = "red",
stroke = F,
fillOpacity = .8,
popup = ~label) %>%
addCircleMarkers(data = outgroup,
fillColor = "blue",
stroke = F,
fillOpacity = .8,
popup = ~name)