0

I try to union two shapefiles in R. I read them by st_read function in sf package.Here is the summary of these two files. enter image description here

  1. I tried to use rbind, it works, but I don't want the black line between the two areas in the right corner. enter image description here

  2. I tried the union function, got a new file with two rows, but cannot plot it,

Error: stat_sf requires the following missing aesthetics: geometry

enter image description here

3
  • The only thing you can do with shapefiles in R is read them in. You can't union them. You would seem to want to know how to union spatial objects, but you've not told us how you've read them in from shapefiles. Did you use st_read from the sf package? You should add this information to your question and show us what the objects are like via the summary or print functions.
    – Spacedman
    Oct 12, 2021 at 21:41
  • Thank you for informing me
    – Zijie Chai
    Oct 12, 2021 at 22:47
  • You can also improve you questions by pasting text instead of images when possible, making simple examples that illustrate your problem using data we can all have (examples from the R help are a good source for this), and showing all the working needed - and no more than is necessary - to duplicate your problem.
    – Spacedman
    Oct 13, 2021 at 6:25

1 Answer 1

1

First I've created two objects that are analagous to yours - these are MULTIPOLYGON geometries but that shouldn't make any difference:

> summary(n1)
     FIPS                    geometry
 Length:1           MULTIPOLYGON :1  
 Class :character   epsg:4267    :0  
 Mode  :character   +proj=long...:0  
> summary(n2)
     FIPS                    geometry
 Length:1           MULTIPOLYGON :1  
 Class :character   epsg:4267    :0  
 Mode  :character   +proj=long...:0  

Each of these is a spatial data frame with one row. Using rbind creates a data frame with two rows, so you see two objects:

plot(st_geometry(rbind(n1,n2)))

enter image description here

To merge them into single o, use st_union:

n12 = st_union(n1,n2)
plot(st_geometry(n12))

enter image description here

Notice you now have a single row and the attributes from both:

> n12
Simple feature collection with 1 feature and 2 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -80.90344 ymax: 36.58965
Geodetic CRS:  NAD27
   FIPS FIPS.1                       geometry
1 37009  37005 POLYGON ((-81.17667 36.4154...
1
  • Thank you so much! It is solved!
    – Zijie Chai
    Oct 13, 2021 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.