#loading packages
library(leaflet)
library(geojsonio)
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(readxl)
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tmap)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#Data frame of selected districts
district <- c("kathmandu","morang","sunsari","surkhet","jumla",
             "banke","dhanusa","parsa","kaski","baglung","rupandehi",
             "dadeldhura","achham","chitwan")
count <- c(2,1,1,1,1,1,1,1,1,1,1,1,1,1)
visit_dis <- data.frame(district,count)
#dataframe of hospital selected and their location
point_obj <- data.frame(id=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
                        name=c("Nobel Medical College","BPKIHS","Janakpur Zonal Hospital", "National Medical College","TU Teaching Hospital","Chitwan Medical College","Pokhara Regional Hospital",
                                 "Dhaulagiri Zonal Hospital","Nepalgunj Medical College","UCMS","Regional Hospital, Surkhet","Karnali Academy of Health Science",
                                 "Bayalpata Hospital","Dhadheldura Provincal Hospital","Kanti Children's Hospital"), 
                         x=c(87.27016779,87.26834481,85.92357668,84.89635987,85.33189755,84.43065856,83.99928277,83.60034646,81.61748969,83.4487686,81.59949326,82.18303595,81.2222903,80.58700204,85.3284721),
                         y=c(26.48997828,26.81243331,26.73361556,27.01133084,27.73772046,27.68509522,28.21390668,28.26881559,28.05935734,27.48981957,28.59730278,29.28315027,29.22088192,29.29946355,27.73491909))
sf_obj = st_as_sf(point_obj, coords = c("x", "y"), crs = 4326) 
#loading map
d_map <- topojson_read("https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/district.topojson") 
df <- geo_join(d_map, visit_dis, 'district', 'district')
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
p_map <- topojson_read("https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/province.topojson") 
#Visualizing in sf and leaflet
plot(df["count"])

cpal <- colorNumeric("Reds", df$con, na.color = "white", alpha=F, reverse = F)


pal <- colorNumeric(
  palette = "YlGnBu",
  domain = df$count)

leaflet(df) %>% addPolygons(fillColor = df$count, stroke = T,opacity = 1, weight = 1) %>% 
  addLegend("bottomright", pal = pal, values = ~count,
            title = "sites",
            labFormat = labelFormat(prefix = ""),
            opacity = 1
  )
#Using TM_MAP
df$sele <- df$count*0+1
map_np1 = tm_shape(df) + tm_polygons(col = "sele",  legend.show = F, colorNA = "white")

map_np1
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.

m <- map_np1+tm_scale_bar(breaks = c(1))
m+tm_layout(title = "Study Sites", frame = FALSE, bg.color = NA, 
            title.position = c("LEFT", "BOTTOM"))
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
## Warning: First scale_bar breaks value should be 0.

map1 <- map_np1 + tm_compass(type = "8star", position = c("left", "bottom")) +
  tm_scale_bar(breaks = c(0, 100, 300), text.size = 0.3,position = c("left", "bottom"))+ tm_layout(title = "Nepal - Distribution of sites", frame = F, title.position = c("RIGHT", "TOP"))
map1+  tm_shape(sf_obj) +
  tm_text("name",col="blue" ,legend.col.show = F, size=0.8)
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.

tmap_mode("plot")
## tmap mode set to plotting
#Finalizing the plot
study_sites <- tm_shape(df) + tm_polygons(col = "sele",  legend.show = F, colorNA = "white", alpha = 0.5) +                                                       
  tm_polygons() + 
  tm_shape(p_map)+ tm_borders(lwd =2, col="azure4") +                                 
  tm_shape(sf_obj) +                                                                 
  tm_markers( text.just = "top",size=0.55 ) +                                                                     
  tm_text("name",size=0.8, col="brown4")+ 
  tm_compass(type = "8star", position = c("left", "bottom")) +
  tm_scale_bar(breaks = c(0, 100, 200), text.size = 0.3,position = c("left", "bottom")) +
  tm_layout(title = "Study Sites", title.position = c("RIGHT", "TOP"))
study_sites
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
## Warning: Currect projection of shape p_map unknown. Long-lat (WGS84) is assumed.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

tmap_save (study_sites, filename = "~/study_sites.png", bg="transparent") 
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
## Warning: Currect projection of shape p_map unknown. Long-lat (WGS84) is assumed.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
## Warning: Currect projection of shape p_map unknown. Long-lat (WGS84) is assumed.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
## Map saved to C:\Users\Dell\Documents\study_sites.png
## Resolution: 2776.061 by 1588.582 pixels
## Size: 9.253538 by 5.295272 inches (300 dpi)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.