0

I am pretty new to coding and r, and I am working on a modeling project using the Tigris package in r and ggplot2 for visualization. I am trying to construct a primary roads data frame but only using the data for the lower 48 states. In Tigris, primary_roads does not seem to include an argument for states and therein lies my problem. I've managed to painstakingly filter out MOST of the data for Alaska, Hawaii, and Puerto Rico by filtering within the FULLNAME column of primary_roads, but that has involved looking up the local names for highways in those locations on various web sources and manually removing them with a filtering code, but I can't seem to get all of them. I will include the code which I've come up with as well as the ggplot output that has the problem areas circled. Any tips are greatly appreciated. This is sort of just a fun side project that spawned from a class assignment that I enjoyed, so there isn't really a timeline on this, and I'm open to suggestions. I was thinking maybe there is a filter someone has come up with? Or maybe something I can use in ggplot to filter out the geometry in the primary_roads table? Or is there something similar to ArcGIS clip function in ggplot2 that I could use to filter other data from the Tigris package to the boundaries of the lower 48 geometry I create in the code? Thanks in advance and I look forward to working with any help and anyone willing to help me with this.

library(tidyverse)
library(ggplot2)
library(tigris)
library(dplyr)

#combining tigris and dplyr packages to create a geometry
st <- tigris::states() %>%
  dplyr::filter(GEOID < "60") %>% #GEOID's above 60 are territories and islands, etc. So I'm removing them for scaling.
  tigris::shift_geometry()

#removing Alaska and Hawaii from the geometry
lower_48 <- st %>% filter(!NAME %in% c("Alaska", "Hawaii"))


transp <- primary_roads()%>% filter(!FULLNAME %in% c("I- H-201","I- H-1","I- H-3","I- H2","I- H-2","Alaska Fwy","Alaskan Way Viaduct","Minnesota Dr", "Pr- 1","Pr- 10","Pr- 165","Pr- 17","Pr- 18","Pr- 181","Pr- 2","Pr- 20","Pr- 22","Pr- 24","Pr- 26","Pr- 3","Pr- 30","Pr- 52","Pr- 53","Pr- 60","Pr- 66","Pr- 165","Pr-66","   
Pr-165","Pr-165","Carr 5","Pr-165","Pr-165","Carr 5","Carr Militar","Ave Central","Expreso Baldorioty de Castro","Expreso Luis Munoz Rivera","Expreso Las Americas","Expreso Loiza","   
                                                     + Expreso Rafael Martinez Nadal","Expreso Jose de Diego","Expreso Cruz Ortiz Stella","Expreso Jose C Barbosa","Expreso Jose Celso Barbosa","Expreso Roman Baldorioty de Castro","Expreso Cruz Ortíz Stella","Expreso el Caño","    
                                                     + Expreso Río Hondo","Autopista Luis A Ferre","Autopista Luis A Ferré","Kamehameha Hwy","Kalanianaole Hwy","W Moanalua Fwy","Moanalua Fwy","   
                                                     + Lunalilo Fwy","Ave John F Kennedy","W H1 Fwy","  
                                                     + S H2 Fwy","W H3 Fwy","S H2 Fwy","Lunalilo Fwy","Expreso Rafael Martinez Nadal","Expreso Río Hondo","E Parks Hwy","George Parks Hwy","New Seward Hwy","New Seward Hwy","Seward Hwy","Glenn Hwy"))

I have attached the image of the ggplot output I made using these. The red lines are the primary_roads data. Ignore the purple gradient and green areas, as those are other parameters I've already completed the code for. The plot output is almost where I want it to be doing it the long, hard way, but there has to be a better way! Thanks everyone. It's a fun project but I'm sort of stuck on this step.

annotated ggplot map output

I am hoping to have the output only include data for the lower 48 US states. Hopefully someone more experienced with Tigris and/or ggplot2 might have some ideas about it?

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
savalle
  • 1
  • 1

0 Answers0