#load the packages in

library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(leaflet)
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::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)
library(dplyr)
library(ggiraph)
map123 <- st_read("states.geojson")
## Reading layer `states' from data source `/Users/samir/states.geojson' using driver `GeoJSON'
## Simple feature collection with 51 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -170 ymin: 18.92245 xmax: -66.97626 ymax: 71.35256
## Geodetic CRS:  NAD83
pop_dens <- read_csv("state_pop_dens.csv")
## Rows: 52 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): GEO.id, state_name
## dbl (2): GEO.id2, Density
## 
## ℹ 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.
new_pop_dens <- pop_dens %>% 
filter(state_name != "Puerto Rico" & state_name != "Alaska")
new_map <- map123 %>% 
filter(NAME != "Hawaii" & NAME != "Alaska" & NAME != "District of Columbia")
map_data <- new_map %>% 
 left_join(new_pop_dens, by = c("NAME" = "state_name"))
info <- paste0(
  "State: ", map_data$NAME,
  "\nDensity: ", map_data$Density
)

{r map_data graph <- ggplot(data = map_data, aes(fill = Density)) + geom_sf() + geom_sf_interactive(aes(geometry = geometry, tooltip = info)) + labs(title = "Population density by State in the United States of America", subtitle = "Most high density populated states are East", fill="", caption = ("Samir Karimov") + theme(plot.caption = element_text(hjust = 0)) + theme(legend.position = "right") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank()) + theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())) Revision Notes from Amin: Inverse colors, light to mean less and darker to mean more densly populated. Remove lines in background and x-y axis. Caption: Mention source -> like data from, also source of the map, who created thsi (author = me) and the date white outline for the states also can expand the size of the map a little bit particular theme that removes, try something like theme_map/theme_void. Examples in github

map_data
## Simple feature collection with 48 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -124.7318 ymin: 24.54547 xmax: -66.97626 ymax: 49.38436
## Geodetic CRS:  NAD83
## First 10 features:
##    STATEFP STUSPS         NAME      GEO.id GEO.id2 Density
## 1       31     NE     Nebraska 0400000US31      31    23.8
## 2       53     WA   Washington 0400000US53      53   101.2
## 3       35     NM   New Mexico 0400000US35      35    17.0
## 4       46     SD South Dakota 0400000US46      46    10.7
## 5       21     KY     Kentucky 0400000US21      21   109.9
## 6       13     GA      Georgia 0400000US13      13   168.4
## 7       05     AR     Arkansas 0400000US05       5    56.0
## 8       42     PA Pennsylvania 0400000US42      42   283.9
## 9       28     MS  Mississippi 0400000US28      28    63.2
## 10      08     CO     Colorado 0400000US08       8    48.5
##                          geometry
## 1  MULTIPOLYGON (((-104.053 43...
## 2  MULTIPOLYGON (((-122.526 47...
## 3  MULTIPOLYGON (((-109.0452 3...
## 4  MULTIPOLYGON (((-104.0577 4...
## 5  MULTIPOLYGON (((-89.13268 3...
## 6  MULTIPOLYGON (((-85.60516 3...
## 7  MULTIPOLYGON (((-89.7331 36...
## 8  MULTIPOLYGON (((-80.51942 4...
## 9  MULTIPOLYGON (((-91.16607 3...
## 10 MULTIPOLYGON (((-109.0501 4...
graph <- ggplot(data = map_data, aes(fill = Density)) +
  geom_sf() +
  scale_fill_gradient(low = "white", high = "black") +
  geom_sf_interactive(aes(geometry = geometry, tooltip = info, color = "white")) +
   labs(title = "Population density by State in the United States of America",
       subtitle = "Most high density populated states are East",
       fill="",
       caption = "Author: Samir Karimov 12/22/2023\nData from: Kaggle\n Map from: data.gov") +
  theme_void() + 
  theme(plot.caption = element_text(hjust = 0, vjust = 0)) +
  theme(plot.caption = element_text(hjust = 0)) +
  theme(legend.position = "right") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank()) +
  theme(axis.text.x=element_blank(),
      axis.ticks.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks.y=element_blank()) 
ggiraph(code = print(graph))
## Function `ggiraph()` is replaced by `girafe()` and will be removed soon.
dpi <- 300 
width_in <- 2400 / dpi  
height_in <- 3000 / dpi  

ggsave("revised data science midterm.png", plot = graph, width = width_in, height = height_in, dpi = dpi)