ds4cabs / CABS_Smart_Website

1 stars 0 forks source link

Zip Code to City Lookup #11

Open ds4cabs opened 1 month ago

ds4cabs commented 1 month ago

@lhs86 @minjikim65

# Install and load necessary libraries
if (!requireNamespace("zipcodeR", quietly = TRUE)) {
  install.packages("zipcodeR")
}
library('zipcodeR')
library("tidyverse")

# Function to get city from zip code
get_city_from_zip <- function(zip_code) {
  tryCatch({
    zip_data <- search_zip(zip_code)
    return(zip_data$city[1])
  }, error = function(e) {
    return(NA)
  })
}

# Example usage
zip_codes <- c("94014", "94701", "22434", "90001")
cities<-search_state('CA') %>% filter(zipcode %in% zip_codes)
ds4cabs commented 1 month ago

Here is the code for zip code map plotting:

# Install and load necessary libraries
if (!requireNamespace("zipcodeR", quietly = TRUE)) {
  install.packages("zipcodeR")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("sf", quietly = TRUE)) {
  install.packages("sf")
}
if (!requireNamespace("ggmap", quietly = TRUE)) {
  install.packages("ggmap")
}
library(zipcodeR)
library(ggplot2)
library(sf)
library(ggmap)

# Function to get city and coordinates from zip code
get_zip_code_data <- function(zip_code) {
  tryCatch({
    zip_data <- search_zip(zip_code)
    return(zip_data)
  }, error = function(e) {
    return(NA)
  })
}

# Example zip codes
zip_codes <- c("94014", "94701", "22434", "90001")

# Get data for zip codes
zip_data_list <- lapply(zip_codes, get_zip_code_data)
zip_data <- do.call(rbind, zip_data_list)

# Convert zip codes to geographic coordinates
get_coordinates <- function(zip_code) {
  geocode(paste(zip_code, "USA"))
}

# Get coordinates
coords <- do.call(rbind, lapply(zip_codes, get_coordinates))
zip_data$latitude <- coords$lat
zip_data$longitude <- coords$lon

# Load a map of the USA
usa_map <- map_data("usa")

# Plot
ggplot() +
  geom_map(data = usa_map, map = usa_map,
           aes(x = long, y = lat, map_id = region), fill = "lightgray", color = "white") +
  geom_point(data = zip_data, aes(x = longitude, y = latitude), color = "red", size = 3) +
  labs(title = "Zip Code Locations",
       x = "Longitude",
       y = "Latitude") +
  theme_minimal()
ds4cabs commented 1 month ago
library(tigris)
library(tidyverse)
library(sf)

# Define the list of Bay Area zip codes (ensure this list is complete and accurate for your needs)
bay_area_zip_codes <- c("75035","94010","94014","94014","94062","94070","94080","94103","94109","94303","94401","94402","94404","94404","94404","94501","94544","94566","94568","94608","95050","95054","95112","95124")
#bay_area_zip_codes <- read.table("C:\\Users\\Shich\\Downloads\\zip.txt")$V1

# Retrieve ZIP Code Tabulation Areas (ZCTAs) data for the year 2010 for California
zipcodes <- zctas(year = 2010, state = "CA", progress_bar = FALSE)

# Filter for only the Bay Area zip codes
filtered_zipcodes <- zipcodes |> filter(ZCTA5CE10 %in% bay_area_zip_codes)

# Calculate the bounding box of the Bay Area zip codes
bay_area_bbox <- st_bbox(filtered_zipcodes)

# Clip the state data to the extent of the Bay Area zip codes
us_states <- states(class = "sf", progress_bar = FALSE)
ca <- us_states |> filter(STUSPS == "CA")
bay_area <- st_crop(ca, bay_area_bbox)

# Plotting the map focused on the Bay Area
ggplot() +
  geom_sf(data = bay_area, fill = "gray", color = "white") +  # Plot trimmed CA as a base layer
  geom_sf(data = filtered_zipcodes, aes(fill = ALAND10), color = "blue") +  # Highlight specific zip codes
  scale_fill_viridis_c(option = "plasma", guide = guide_legend(title = "Land Area")) +
  labs(title = "Bay Area Zip Codes by Land Area",
       subtitle = "Land area data from 2010 ZCTAs") +
  theme_void() +
  theme(legend.position = "bottom")

image

ds4cabs commented 1 month ago

94014 94306 94402 95124 94568 94403 94404 94404 94404 94538 95112 94014 94530 94587 94116 94103 94544 94070 94010 94403 95014 94030 75035 94404 94404 94080 94109 94080 94112 94538 94501 94608 94566 95050 94062 94401 94303 95054