R and Leaflet (a JavaScript library) to plot schools on a very simple map and show the boundaries of local authority districts.

#install.packages("httpuv")
#install.packages("leaflet")
#install.packages("dplyr")
library(leaflet)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)

R and Leaflet : MyHome

r_MyHome <- leaflet() %>%
  addTiles() %>%            
  addMarkers(lng=90.409739, lat=23.757419,
             popup="MyHome")
r_MyHome

Map data

https://data.humdata.org/dataset/bangladesh-healthsites#

library(readr)
bangladesh <- read_csv("C:/Users/user/Desktop/bangladesh.csv")
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   X = col_double(),
##   Y = col_double(),
##   osm_id = col_double(),
##   completeness = col_double(),
##   is_in_health_zone = col_logical(),
##   water_source = col_logical(),
##   changeset_id = col_double(),
##   insurance = col_logical(),
##   staff_doctors = col_logical(),
##   contact_number = col_double(),
##   electricity = col_logical(),
##   operational_status = col_logical(),
##   is_in_health_area = col_logical(),
##   health_amenity_type = col_logical(),
##   changeset_version = col_double(),
##   addr_postcode = col_double(),
##   staff_nurses = col_logical(),
##   beds = col_double(),
##   url = col_logical()
## )
## See spec(...) for full column specifications.
## Warning: 9 parsing failures.
##  row                 col               expected           actual                                   file
## 1225 addr_postcode       a double               à§ÂĢৎà§ÂĒà§ÂĻ     'C:/Users/user/Desktop/bangladesh.csv'
## 1304 contact_number      no trailing characters -8112856         'C:/Users/user/Desktop/bangladesh.csv'
## 1565 contact_number      no trailing characters , +8801611488026 'C:/Users/user/Desktop/bangladesh.csv'
## 1620 contact_number      no trailing characters -003686          'C:/Users/user/Desktop/bangladesh.csv'
## 2355 health_amenity_type 1/0/T/F/TRUE/FALSE     dialysis;x_ray   'C:/Users/user/Desktop/bangladesh.csv'
## .... ................... ...................... ................ ......................................
## See problems(...) for more details.
dhaka <- bangladesh %>% 
  filter(addr_city=="Dhaka")%>%
  filter(!(X=="NA"))

Add data

m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addCircles(~X, ~Y, popup=dhaka$name) 
m

Different view : CartoDB.DarkMatter

m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addCircles(~X, ~Y, popup=dhaka$name)  %>% addProviderTiles(providers$CartoDB.DarkMatter)
m

Different view : Stamen.Terrain

m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addCircles(~X, ~Y, popup=dhaka$name)  %>% 
  addProviderTiles("Stamen.Terrain")
m

Different view : OpenTopoMap

m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addProviderTiles(providers$OpenTopoMap)%>% 
  addMarkers(lng=90.409739, lat=23.757419,
             popup="MyHome")
m

http://leaflet-extras.github.io/leaflet-providers/preview/index.html

Change style of Circles

m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addCircles(~X, ~Y,
 popup=dhaka$name,
weight = 13, 
radius=4, 
stroke = T, 
fillOpacity = 0.5,
color="#ffa500")
m

Color Factor

pal <- colorFactor(c("green", "red","blue","black","navy"), domain = c("clinic", "hospital","dentist", "doctors","pharmacy" ))
leaflet(dhaka) %>%
  addTiles() %>% 
  addCircleMarkers(
    ~X, ~Y,
    color = ~pal(amenity),
    stroke = TRUE, fillOpacity = 0.5,
                    popup=~amenity,
                    label=~as.character(amenity)
  )

Add Marker

m <- leaflet(dhaka) %>%
  addTiles() %>% 
  addProviderTiles("Stamen.Terrain")%>%
  addCircles(~X, ~Y, popup=dhaka$name,weight = 13, radius=4, stroke = T, fillOpacity = 0.5)%>%
  addAwesomeMarkers(lng=~X, lat=~Y, 
                    popup=~amenity,
                    label=~as.character(amenity))
m

Add popup -1

content <- paste(sep = "<br/>",
  "<b><a href='https://www.linkedin.com/in/naimulislam19862020/'>Naimul Islam</a></b>",
  "324,Mogbazar",
  "Dhaka-1200"
)
m <- leaflet() %>%
  addTiles() %>% 
  addProviderTiles("Stamen.Terrain")%>%
  addPopups(lng=90.409739, lat=23.757419, content,
    options = popupOptions(closeButton = FALSE)
  )
m

Add popup -2

content <- paste(sep = "<br/>",
  "<b><a href='https://www.linkedin.com/in/naimulislam19862020/'>Naimul Islam</a></b>",
  dhaka$X,
  dhaka$Y
)
m <- leaflet(dhaka) %>%
  addTiles() %>% 
  addProviderTiles("Stamen.Terrain")%>%
  addPopups(lng=~X, lat=~Y, content,
    options = popupOptions(closeButton = FALSE)
  )
m

Add popup -3

content <- paste(sep = "<br/>",
  "<b><a href='https://en.wikipedia.org/wiki/List_of_hospitals_in_Bangladesh'>List of Health Facility</a></b>",
  dhaka$name,
  dhaka$amenity
)
m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addCircles(~X, ~Y, popup=content) 
m

Add Level

content <- paste(sep = "<br/>",dhaka$name,dhaka$amenity)
m <- leaflet(dhaka) %>%
  addTiles() %>%  
  addMarkers(~X, ~Y, popup=content,
    label = ~name,
    labelOptions = labelOptions(noHide = F, direction = "bottom",
      style = list(
        "color" = "red",
        "font-family" = "serif",
        "font-style" = "italic",
        "box-shadow" = "3px 3px rgba(0,0,0,0.25)",
        "font-size" = "12px",
        "border-color" = "rgba(0,0,0,0.5)") ))
m

Leaflet Map Draw

# make a simple track line
lin = data.frame(lon = c(-65.17536, -65.37423, -65.64541, -66.06122, -66.15161),
                 lat = c(43.30837, 42.94679, 42.87448, 42.92871, 42.72985))
 
# make a few points
pts = data.frame(lon = c(-65.3, -65.7, -64.1),
                 lat = c(43.4, 43, 42.9))
 
# build a polygon (in this case the 'Roseway Basin Area To Be Avoided')
ply = data.frame(lon = c(-64.916667, -64.983333, -65.516667, -66.083333),
                 lat = c(43.266667, 42.783333, 42.65, 42.866667))

# required libraries

library(mapview, quietly = T, warn.conflicts = F)
 
# start basemap (note the argument to hide the zoom buttons)
map <- leaflet(options = leafletOptions(zoomControl = FALSE)) %>% 
  
  # add ocean basemap
  addProviderTiles(providers$Esri.OceanBasemap) %>%
  
  # focus map in a certain area / zoom level
  setView(lng = -65, lat = 43, zoom = 7) %>%
  
  # add inset map
  addMiniMap(
    tiles = providers$Esri.OceanBasemap,
    position = 'topright', 
    width = 200, height = 200,
    toggleDisplay = FALSE) %>%
  
  # add graticules with nice labels (recommended for static plot)
  addSimpleGraticule(interval = 2) %>%
  
  # add graticules from a NOAA webserver (recommended for interactive plot)
  # addWMSTiles(
  #   "https://gis.ngdc.noaa.gov/arcgis/services/graticule/MapServer/WMSServer/",
  #   layers = c("1-degree grid", "5-degree grid"),
  #   options = WMSTileOptions(format = "image/png8", transparent = TRUE),
  #   attribution = NULL,group = 'Graticules') %>%
  
  # add points (as circle markers)
  addCircleMarkers(data = pts, ~lon, ~lat,
                   weight = 0.5,
                   col = 'black', 
                   fillColor = 'darkslategrey',
                   radius = 4, 
                   fillOpacity = 0.9, 
                   stroke = T, 
                   label = ~paste0('Point at: ', 
                                   as.character(round(lat,3)), ', ', 
                                   as.character(round(lon,3))), 
                   group = 'Points') %>%
  
  # add lines
  addPolylines(data = lin, ~lon, ~lat,
               weight = 3,
               color = 'red',
               popup = 'This is a line!', 
               smoothFactor = 3,
               group = 'Lines') %>%
  
  # add polygons
  addPolygons(data=ply, lng=~lon, lat=~lat,
              weight = 1, 
              color = 'grey', 
              fillColor = 'grey',
              fill = T, 
              fillOpacity = 0.25, 
              stroke = T, 
              dashArray = c(5,5), 
              smoothFactor = 3,
              options = pathOptions(clickable = F),
              group = 'Polygons')
 
# show map
# map
 
# save map as static image
# mapshot(map, file = 'leaflet_map.png')
map