Intro Thoughts

Status Quo

Experiment

library(tidyverse)


compute_panel_state <- function(data, scales){

usmapdata::us_map() %>% 
    rename(geometry = geom) %>% # includes geometry column
    ggplot2::StatSf$compute_panel(coord = ggplot2::CoordSf) |>
    ggplot2::StatSfCoordinates$compute_group(coord = ggplot2::CoordSf) |>
    inner_join(data)

}

StatUSstates <- ggproto("StatUSstates", 
                             Stat,
                             compute_panel = compute_panel_state,
                             default_aes = aes(label = after_stat(full)),
                             required_aes = c("full|abb|fips"))

USArrests |>
  rownames_to_column("state") |># two var data - no geometry column with borders
  ggplot() +
  aes(full = state) +
  geom_sf(stat = StatUSstates) +
  geom_sf_text(stat = StatUSstates, check_overlap = T) +
  aes(fill = Murder)
## Warning in layer_sf(data = data, mapping = mapping, stat = stat, geom =
## GeomText, : Ignoring unknown parameters: `fun.geometry`
## Joining with `by = join_by(full)`
## Joining with `by = join_by(full)`

#And if you will use a bunch,then can do as follows, and set crs create in-script convenience functions...
usmapcrs <- sf::st_crs(usmapdata::us_map())
geom_state <- function( ...){c(geom_sf(stat = StatUSstates, ...), 
                               coord_sf(crs = usmapcrs))}
geom_state_text <- function( ...){c(geom_sf_text(stat = StatUSstates,...),
                                    coord_sf(crs = usmapcrs))}
geom_state_label <- function( ...){c(geom_sf_label(stat = StatUSstates,...),
                                   coord_sf(crs = usmapcrs))}

USArrests |>
  rownames_to_column("state") |># two var data - no geometry column with borders
  ggplot() +
  aes(full = state) +
  geom_state() + 
  geom_state_text() + 
  aes(fill = Murder) + 
  aes(label = Murder) + 
  scale_fill_viridis_c()
## Warning in layer_sf(data = data, mapping = mapping, stat = stat, geom =
## GeomText, : Ignoring unknown parameters: `fun.geometry`
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Joining with `by = join_by(full)`
## Joining with `by = join_by(full)`

Closing remarks, Other Relevant Work, Caveats