Code
library(tidyverse)
library(rvest)
library(chromote)
library(janitor)
library(tidygeocoder)
library(leaflet)
library(sf)
with leaflet
Tony Duan
US location data ::: {.cell}
:::
List of 4
$ x : num [1:15599] -87.5 -87.5 -87.5 -87.5 -87.6 ...
$ y : num [1:15599] 30.4 30.4 30.4 30.3 30.3 ...
$ range: num [1:4] -124.7 -67 25.1 49.4
$ names: chr [1:63] "alabama" "arizona" "arkansas" "california" ...
- attr(*, "class")= chr "map"
US 2-22 GDP data
Rows: 51
Columns: 3
$ names <chr> "California", "Texas", "New York", "Florida", "Illinois…
$ total_2022_usd <dbl> 3641643, 2402137, 2048403, 1439065, 1025667, 911813, 82…
$ per_2022_usd <dbl> 92190, 78456, 104344, 62446, 82126, 71160, 69978, 69253…
Rows: 51
Columns: 6
$ name <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "California…
$ id <chr> "01", "02", "04", "05", "06", "08", "09", "10", "11", "…
$ density <dbl> 94.650, 1.264, 57.050, 56.430, 241.700, 49.330, 739.100…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-87.3593 35..., MULTIPOLYG…
$ total_2022_usd <dbl> 281569, 65699, 475654, 165989, 3641643, 491289, 319345,…
$ per_2022_usd <dbl> 54753, 86722, 62365, 54259, 92190, 82954, 88760, 85977,…
#pal <- colorNumeric("magma", NULL)
pal <- colorNumeric(
palette = "Blues",
domain = map_df002$per_gdp_usd
)
binpal <- colorBin("Blues",map_df002$per_gdp_usd, 20, pretty = FALSE)
m <- leaflet(map_sf002) %>%
# Add default OpenStreetMap map tiles
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
#fillColor = ~ pal(per_2022_usd)
#color = ~pal(per_2022_usd)
color = ~binpal(per_2022_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_2022_usd, "(千 美元)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002$per_2022_usd/1000), "K <br/>",
"总gpd 美元:", round(map_sf002$total_2022_usd), "M <br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_2022_usd, opacity = 1.0
)
m
https://www.gunviolencearchive.org/reports/mass-shooting?page=8&year=2023
---
title: "US map"
subtitle: "with leaflet"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
# US gdp by State
```{r}
library(tidyverse)
library(rvest)
library(chromote)
library(janitor)
library(tidygeocoder)
library(leaflet)
library(sf)
```
```{r}
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
```
US location data
```{r}
json_data=read_sf("us-states.json")
```
```{r}
map_df <- as.data.frame(json_data)
```
```{r}
glimpse(mapStates)
```
US 2-22 GDP data
```{r}
library(openxlsx)
library(readxl)
per_gdp_usd=read_excel('US state gpd 2022.xlsx') %>% mutate(names=str_replace(names,'\\*','') %>% str_trim() )
```
```{r}
glimpse(per_gdp_usd)
```
```{r}
map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "names")
```
```{r}
glimpse(map_df002)
```
```{r}
map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
```
```{r}
#pal <- colorNumeric("magma", NULL)
pal <- colorNumeric(
palette = "Blues",
domain = map_df002$per_gdp_usd
)
binpal <- colorBin("Blues",map_df002$per_gdp_usd, 20, pretty = FALSE)
m <- leaflet(map_sf002) %>%
# Add default OpenStreetMap map tiles
addTiles() %>%
addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1,
#fillColor = ~ pal(per_2022_usd)
#color = ~pal(per_2022_usd)
color = ~binpal(per_2022_usd)
,popup = ~ paste0(
"地区:", name, "<br/>",
"<hr/>",
"人均gpd:", per_2022_usd, "(千 美元)", "<br/>"
)
,label = lapply(paste0(
"地区:", "<b>", map_sf002$name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002$per_2022_usd/1000), "K <br/>",
"总gpd 美元:", round(map_sf002$total_2022_usd), "M <br/>"
), htmltools::HTML)
)%>% addLegend(
position = "bottomright", title = "人均gpd(美元)",
pal = pal, values = ~per_2022_usd, opacity = 1.0
)
m
```
# resouce:
https://www.gunviolencearchive.org/reports/mass-shooting?page=8&year=2023