zoom - Leaflet in R: SetView based on range of latitude and longitude from dataset -
in ggmap, can set view of map based on longitude , latitude of input data, 2 columns of csv longitude , latitude, i.e.
sep <- read.csv("31r_sep_assets_csv.csv") # map bbox <- make_bbox(sep$longitude, sep$latitude, f = 0.3) map <- get_map(bbox)
i searching similar function in leaflet, far, find setview() takes in actual value latitude , longitude, i.e.
m <- leaflet() %>% setview(lng = -71.0589, lat = 42.3601, zoom = 12) m %>% addtiles()
what function use?
here's code far
library(ggmap) library(ggplot2) library(historydata) library(leaflet) library(rgdal) setwd("d:/gis/31r") sep <- read.csv("31r_sep_assets_csv.csv") sub1 <- sep[grep("sep.12", names(sep))] sep$newcol <- 100*rowsums(sub1)/rowsums(sep[4:7]) # create new grouping variable percent_sep12_assets <- ifelse(sep[,8] <= 33, "less 33%", ifelse(sep[,8] >= 66, "more 66%", "between 33% , 66%")) leaflet(data = sep[]) %>% addtiles() %>% addmarkers(~longitude, ~latitude, popup = ~as.character(paste(site, percent_sep12_assets , sep=", ")))
and output zoomed out way
dput(sep) structure(list(site = structure(1:5, .label = c("staten island\\31r001", "staten island\\31r002", "staten island\\31r003", "staten island\\31r004", "staten island\\31r005"), class = "factor"), latitude = c(40.508874, 40.577256, 40.520825, 40.552373, 40.529697), longitude = c(-74.244048, -74.100135, -74.211845, -74.195516, -74.187532), windows.sep.11 = c(63l, 174l, 11l, 85l, 163l), mac.sep.11 = c(0l, 1l, 4l, 0l, 0l), windows.sep.12 = c(124l, 185l, 9l, 75l, 23l), mac.sep.12 = c(0l, 1l, 32l, 1l, 0l), newcol = c(66.3101604278075, 51.5235457063712, 73.2142857142857, 47.2049689440994, 12.3655913978495 )), .names = c("site", "latitude", "longitude", "windows.sep.11", "mac.sep.11", "windows.sep.12", "mac.sep.12", "newcol"), row.names = c(na, -5l), class = "data.frame")
update
tried
addtiles(do.call(fitbounds, args = c(list(map = leaflet()), as.list(setnames(make_bbox(sep$longitude, sep$latitude, f = 0.3), c("lng1", "lat1", "lng2", "lat2")))))
and map blank
then tried
leaflet(data = sep[]) %>% addtiles() %>% fitbounds(~min(sep$longitude), ~min(sep$latitude), ~max(sep$longitude), ~max(sep$latitude)) %>% addmarkers(~longitude, ~latitude, popup = ~as.character(paste(site, percent_sep12_assets , sep=", ")))
and map still zoomed out ...
workaround
i using setview uses average of longitude , latitude
leaflet(data = sep[]) %>% setview(lng = mean(sep$longitude), lat = mean(sep$latitude), zoom = 12) %>% addtiles() %>% addmarkers(~longitude, ~latitude, popup = ~as.character(paste(site, percent_sep12_assets , sep=", ")))
seems work far ....
use
fitbounds(lng1 = min(table$lng), lat1 = min(table$lat), lng2 = max(table$lng), lat2 = max(table$lat))
Comments
Post a Comment