0

I have three different spatial dataframes that I'm trying to combine as one big one. The reproducible code looks like this:

library(sf)
library(sp)
library(leaflet)
library(tigris)
library(rgdal)
library(raster)
library(dplyr)


# Dataset 1 ---------------------------------------------------------------

set1 <- voting_districts("nevada") 
set1 <- subset(set1,set1$COUNTYFP10 != '003')
set1 <- subset(set1,set1$COUNTYFP10 != '031')

# Dataset 2 ---------------------------------------------------------------

#Dataset Download Link:
#https://opendata.arcgis.com/datasets/3e738fdaca2b4aeba0c18a2478cae956_0.zip?outSR=%7B%22wkid%22%3A102707%2C%22latestWkid%22%3A3421%7D

dsn <- "./Precincts"
set2 = readOGR(dsn)


# Dataset 3 ---------------------------------------------------------------

#Dataset Download Link:
#https://opendata.arcgis.com/datasets/fcab7fcd0b2e4243b4f6d7292c2e4daa_7.zip?outSR=%7B%22latestWkid%22%3A3423%2C%22wkid%22%3A102709%7D

dsn <- "./Voter_Districts"
set3 = readOGR(dsn)


# Transform ---------------------------------------------------------------

set1 <- spTransform(set1, CRS("+init=epsg:4326"))
set2 <- spTransform(set2, CRS("+init=epsg:4326"))
set3 <- spTransform(set3, CRS("+init=epsg:4326"))


# Plot --------------------------------------------------------------------

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons( fillColor = "red",
               color = "black",
               weight = 1.0, data = set1) %>%
  addPolygons( fillColor = "blue",
               color = "black",
               weight = 1.0, data = set2) %>%
  addPolygons(fillColor = "green",
              color = "black",
              weight = 1.0, data = set3)

What I would like to do is combine them all into one big SpatialPolygonsDataFrame that would allow me to do the same plot as above, but without the need for two additional addPolygons functions. Something like this:

leaflet(combined_sets) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(...)

Is that even possible? Given that they are three different datasets with different columns, I'm not sure if it can be done.

2
  • Hi, sorry but without the files it is not reproducible. But could it be that this link can help you? [gis.stackexchange.com/questions/155328/… Commented Sep 3, 2019 at 22:45
  • @JohannesStötzer I did include the files. set2 and set3 include the download link.
    – user9302275
    Commented Sep 3, 2019 at 23:13

1 Answer 1

0

As said in the comment, there are soltions. Both bindand raster::unionworked for me

ab <- bind(set2,set3)
ab <- raster::union(set2,set3)

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons( fillColor = "blue",
               color = "black",
               weight = 1.0, data = ab)

enter image description here