# These are the libraries I have loaded which I will use to do things not possible with just base R
library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2) # You need this for any plotting, mapping, etc.
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, will retire 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:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(leaflet) # Also needed for spacial mapping
library(ggthemes)
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired during October 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-7, (SVN revision 1203)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.3, released 2022/10/21
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 9.1.0, September 1st, 2022, [PJ_VERSION: 910]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(broom)
library(tigris) # Super important: the source for the county borders/shapefile
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(dplyr)
library(sf) #used for geom_sf
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(scales) # used for the coloring scale of the choropleth
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(ggiraph) # used for making the final product interactive + hoverable
# This is how I read in a dataset, which is where my data and numbers will come from
dataset <- read_csv("~/Desktop/COMP_STAT_112/midterm/Electric_Vehicle_Population_Data.csv")
## Rows: 153830 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): VIN (1-10), County, City, State, Make, Model, Electric Vehicle Typ...
## dbl (7): Postal Code, Model Year, Electric Range, Base MSRP, Legislative Di...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# and this is the shapefile used to determine borders
wa_counties <- counties(state = "Washington", cb = FALSE, resolution = "500k", year = 2022)
# I didnt end up using this one, but it's another example.
borders <- read_csv("~/Desktop/COMP_STAT_112/midterm/united_states_washington_administrative_boundaries_level6_counties_polygon/united_states_washington_administrative_boundaries_level6_counties_polygon.csv")
## Rows: 39 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): WKT, boundary, name
## dbl (5): gid, admin_level, population, z_order, way_area
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
electric_vehicle_count <- dataset %>%
group_by(County) %>%
summarise(EV_Count = n())
electric_vehicle_count <- electric_vehicle_count %>%
rename(name = County)
# This function pastes text into the same place in every row
electric_vehicle_count$name <- paste(electric_vehicle_count$name, "County")
# This function renames a column in the specified dataset
ev_count2 <- electric_vehicle_count %>%
rename(NAMELSAD = name)
# This function, merge, merges two datasets together based on a common column
merged2 <- merge(wa_counties, ev_count2, by= "NAMELSAD", all.x = TRUE)
merge2 <- merge(wa_counties, ev_count2, by= "NAMELSAD", all.x = TRUE)
# These two functions convert the merged2 data to numeric instead of integer, and then takes the log of the numbers in the EV_Count column.
merged2$EV_Count <- as.numeric(merged2$EV_Count)
merged2$EV_Count <- log(merged2$EV_Count)
color_limits <- c(0, max(merged2$EV_Count))
color_breaks <- seq(0, max(merged2$EV_Count), length.out = 6)
p <- ggplot(merged2) +
geom_sf_interactive(aes(fill = EV_Count, tooltip = paste(NAMELSAD, "\nEV Count:", round(merge2$EV_Count, 0)))) +
scale_fill_viridis_c(
limits = color_limits,
breaks = color_breaks
) +
labs(
title = "Number of Electric Vehicles in WA state counties",
subtitle = "graph by Gavia Boyden, dataset from Data.gov",
fill = "Log of the number of Electric Vehicles in each County"
) +
theme_void() +
theme(legend.position = "bottom",
legend.text = element_text(angle = 45, hjust = 1),
legend.title = element_text(hjust = 0.5, vjust = 1)
)
girafe(code = print(p))