I have three spatial layers, 1 USA boundaries and 2 point layers for Arby's and Hardee's locations across USA. Goal of the assignment is to create 2 choropleth maps for density of these fast food joints shown by state.

What I am able to achieve so far;

#importing layers

usa <- read_sf("/Users/username/Desktop/r-intro/data/seminarClass/usa.shp")
arbys <- sf::st_read("/Users/username/Desktop/r-intro/data/seminarClass/restaurants.gdb", layer="arbys")
hardees <- sf::st_read("/Users/username/Desktop/r-intro/data/seminarClass/restaurants.gdb", layer="hardees")

#crs check

usacrs <- st_crs(usa) # "ESRI",102003

arbyscrs <- st_crs(arbys) # "EPSG",4269
hardeescrs <- st_crs(hardees) # "EPSG",4269
usageom <- (usa$geometry) #usa geometry

#crs matching

arbys_usa <- arbys %>% st_set_crs(st_crs(usa))
hardees_usa <- hardees %>% st_set_crs(st_crs(usa))


arbyJoin <- usa %>% st_join(arbys_usa) %>% group_by(STATE_FIPS) %>%
hardeeJoin <- usa %>% st_join(hardees_usa) %>% group_by(STATE_FIPS) %>%

All coding except the part in spatial join is done by me and I understand what's going on in the code. I emailed my instructor in order to ask for help and he send me this line shown in the spatial join section of my code;

arbyJoin <- usa %>% st_join(arbys_usa) %>% group_by(STATE_FIPS) %>%

I don't understand exactly what's going on in this part therefore I can't figure out what to do with the summarise part.

I have tried

summarise(arbyJoin, sum=(STATE_NAME))


summarise(arbyJoin, sum=(geometry))

I don't necessarily get an error but I don't understand if they work or not as well, since I don't understand the code.

Can anyone shed some light on this line of the code and point me the right direction on how to proceed with this summarise function?

Ultimate result I'm trying to achieve is to create a choropleth map using these joint layers.

  • 1
    Hey, having a look to your code, I think you should use st_transform instead of st_set_crs. Regarding your question, that line should merge the geometries that shares the STATE_FIPS variable (I.e. one row per state). However I don’t fully understand how is this related with the join part of your question. Hope that helps – dieghernan Sep 25 '20 at 17:53
  • Thank you, what I don't understand is my point layer doesn't have STATE_FIPS variable/column but only lat/long as a vector column. When I ran that line I don't get an error so I use summarize(arbyJoin, sum=STATE_FIPS) to calculate the amount of restaurants in each state but I don't see any newly calculated columns. – puredata Sep 25 '20 at 18:22
  • 1
    And on your USA data? The column should be anywhere, otherwise you would get an error – dieghernan Sep 25 '20 at 19:27
  • My USA data has the STATE_FIPS column. Other colleagues were able to map using this data. – puredata Sep 25 '20 at 19:31
  • 1
    So your workflow is USA(STATE_FIPS) joined (spatially) with points and summarised by state. Are you familiar with the pipe %>%? The summarise go after the join, so it’s done over the merged dataset, which contains both the records of USA and the points. I suggest you to go step by step ,having a look to the number of rows of the result and plotting each step. Good luck – dieghernan Sep 26 '20 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.