library(data.table)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::between() masks data.table::between()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks data.table::first()
## ✖ lubridate::hour() masks data.table::hour()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks data.table::last()
## ✖ lubridate::mday() masks data.table::mday()
## ✖ lubridate::minute() masks data.table::minute()
## ✖ lubridate::month() masks data.table::month()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ lubridate::second() masks data.table::second()
## ✖ purrr::transpose() masks data.table::transpose()
## ✖ lubridate::wday() masks data.table::wday()
## ✖ lubridate::week() masks data.table::week()
## ✖ lubridate::yday() masks data.table::yday()
## ✖ lubridate::year() masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(tidyverse)
library(gplots)
##
## Attaching package: 'gplots'
##
## The following object is masked from 'package:stats':
##
## lowess
library(maps)
##
## Attaching package: 'maps'
##
## The following object is masked from 'package:purrr':
##
## map
library(ggmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(RColorBrewer)
library(leaflet)
library(ggthemes)
library(usdata)
geojson_path <- "Desktop/California_County_Boundaries.geojson"
county_data <- st_read(geojson_path)
## Reading layer `cnty19_1' from data source
## `/Users/holdbrookk23/Desktop/California_County_Boundaries.geojson'
## using driver `GeoJSON'
## Simple feature collection with 69 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -124.4098 ymin: 32.53429 xmax: -114.1308 ymax: 42.0095
## Geodetic CRS: WGS 84
csv_path <- "Desktop/CAcounty.csv"
data <- read.csv(csv_path)
library(data.table)
library(sf)
library(tidyverse)
county_data1 <- county_data %>%
semi_join(data, by = c("COUNTY_NAME"="COUNTY_NAME"))
cacounties <- county_data1[!duplicated(county_data1$COUNTY_NAME),]
countyca <- filter(county,
state=='California')
countyca$name <- sub("County$", "", countyca$name)
colnames(countyca)[colnames(countyca)== "name"] <- "COUNTY_NAME"
countyca$OBJECTID <- seq_along(countyca$COUNTY_NAME)
county_data2 <- cacounties %>%
full_join(countyca, by = c("OBJECTID" = "OBJECTID"))
library(ggplot2)
library(tidyverse)
library(gplots)
library(maps)
library(ggmap)
library(RColorBrewer)
library(leaflet)
library(ggthemes)
library(usdata)
ggplot()+
geom_sf(data=county_data2, aes(fill=per_capita_income))+
scale_fill_viridis_c()+
labs(title= "California Map",
fill="per capita income")+
theme_minimal()

map <- leaflet() %>%
addTiles()
colors <- colorNumeric(palette = "BuPu", domain = county_data2$per_capita_income)
map <- map %>%
addPolygons(
data = county_data2,
fillColor = ~colors(per_capita_income),
fillOpacity = 0.7,
color = "white",
weight = 1,
highlight = highlightOptions(
weight = 3,
color = "black",
bringToFront = TRUE
),
popup = ~paste("County:", COUNTY_NAME.x, "<br>Per Capita Income:", per_capita_income),
label = ~paste("Per Capita Income:", per_capita_income))
map <- map %>%
addControl(
html = '<div style="text-align: center; font-size: 18px; font-weight: bold;">California Per Capita Income by County</div>',
position = "topright"
)
map <- map %>%
addLegend(
pal = colors,
values = county_data2$per_capita_income,
position = "bottomleft",
title = "Per Capita Income ($)",
labFormat = labelFormat()
)
centroids <- st_centroid(county_data2)
## Warning: st_centroid assumes attributes are constant over geometries
label_data <- data.frame(
lng = st_coordinates(centroids)[, 1],
lat = st_coordinates(centroids)[, 2],
label = county_data2$COUNTY_NAME.x
)
map <- map %>%
addLabelOnlyMarkers(
data = label_data,
lng = ~lng,
lat = ~lat,
label = ~label,
labelOptions = labelOptions(
noHide = TRUE,
textOnly = TRUE,
style = list(
"font-weight" = "bold",
"color" = "black",
"background-color" = "transparent",
"border" = "transparent",
"padding" = "1px",
"border-radius" = "0.5px"
),
textsize = "7px"
)
)
map