Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Counting how many point are in the polygon of region [R]

$
0
0

I would like to ask how to calculate number of point that are in some region when we have longtitue and latitude variables of point and polygon of country and its regions.

I provided example below: I would like to calculate how many point are in what regions (including zero when there is no point) and than add this variables to data2@data object so one can use count measures to fill each regions polygons.

library(leaflet)
library(tidyverse)
set.seed(101)

URL2 <- "https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_FRA_2_sp.rds"
data2 <- readRDS(url(URL2))


random_long_lat <- 
  data.frame(
  long = sample(runif(100, min = 1.3, max = 1.99), replace = F),
  lat = sample(runif(100, min = 42.69, max = 49.75), replace = F)
  )


leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons(data=data2, stroke = TRUE, color = "black", weight="", smoothFactor = 0.95, 
              fill = F) %>% 
  addCircles(lng = random_long_lat$long, lat = random_long_lat$lat)

# Here add new variable called count, that would count how many point are in the region
data2@data  

I would like the result so one can calculate something like this:

data2@data <- 
  data2@data %>% 
  mutate(counts = rnorm(nrow(data2), 100, sd = 20))

cuts <- quantile(data2$counts, probs = seq(0, 1, 0.2))
cuts <- colorBin("Greens", domain = data2$counts, bins = cuts)

leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addPolygons(data=data2, stroke = TRUE, color = "white", weight="", smoothFactor = 0.95, 
                fillOpacity = 0.65, fillColor = ~cuts(data2$counts)) %>% 
  addLegend(pal = cuts, 
            values = data2$hdp,
            labFormat = labelFormat(suffix = ""),
            opacity = 0.85, title = "How many point were counted in each region", position = "topright")


however I dont know is it posible to calculate number of point in each regions having just coordinances?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>