options(repos = c(CRAN = "https://cran.r-project.org"))
if (!require("dplyr")) {
    install.packages("dplyr", repos = "https://cran.r-project.org")
}
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dplyr)

library(readxl)
library(dplyr)

AHEAD_data <- read_excel("C:\\Users\\koltn\\Desktop\\Work\\R_Studio_Class\\AHEAD_data.xlsx", sheet = "State totals")

View(AHEAD_data)

filtered_data <- AHEAD_data %>%
  filter((statistic =="percentage") |
           (statistic == "count" & indicator %in% c("Diagnoses", "Incidence")))

View(filtered_data)
install.packages("ggplot2")
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\koltn\AppData\Local\Temp\RtmpY7Luti\downloaded_packages
install.packages("sf")
## package 'sf' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'sf'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\koltn\AppData\Local\Programs\R\R-4.4.2\library\00LOCK\sf\libs\x64\sf.dll
## to C:\Users\koltn\AppData\Local\Programs\R\R-4.4.2\library\sf\libs\x64\sf.dll:
## Permission denied
## Warning: restored 'sf'
## 
## The downloaded binary packages are in
##  C:\Users\koltn\AppData\Local\Temp\RtmpY7Luti\downloaded_packages
install.packages("tigris")
## package 'tigris' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\koltn\AppData\Local\Temp\RtmpY7Luti\downloaded_packages
library(readxl)
library(ggplot2)
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(dplyr)
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
options(tigris_use_cache = TRUE, tigris_class = "sf")

states <- states(cb = TRUE)
## Retrieving data for the year 2021
alaska <- states[states$STUSPS == "AK", ]
alaska <- st_transform(alaska, crs = st_crs(4326)) 
alaska$geometry <- lapply(alaska$geometry, function(g) { 
  g <- g*0.35
  g <- g + c(-35, -5)
  g
  })

hawaii <- states[states$STUSPS == "HI", ]
hawaii <- st_transform(hawaii, crs = st_crs(4326))
hawaii$geometry <- lapply(hawaii$geometry, function(g) {
  g <- g + c(54, -5)
  g
})

states[states$STUSPS == "AK", ]$geometry <- alaska$geometry
states[states$STUSPS == "HI", ]$geometry <- hawaii$geometry

map_data <- left_join(states, AHEAD_data, by = c("NAME" = "state"))

plot_data <- map_data %>%
filter(metric == "Estimated incidence", statistic == "count")

p <- ggplot(plot_data) + 
  geom_sf(aes(fill = value), color = "white") +
  scale_fill_viridis_c(option = "C") +
  theme_minimal() +
  labs(title = "Map for Metric: Estimated incidence", fill = "value")

print(p)

options(repos = c(CRAN = "https://cran.r-project.org"))
if (!require("dplyr")) {
    install.packages("dplyr", repos = "https://cran.r-project.org")
}
library(dplyr)