library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
download.file("https://raw.githubusercontent.com/jfrench/DataWrangleViz/master/06-interactive-graphics-with-plotly.html", "06-interactive-graphics-with-plotly.html")

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

head(nc[c("NAME", "BIR74", "geometry")], n = 3)
## Simple feature collection with 3 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
## Geodetic CRS:  NAD27
##        NAME BIR74                       geometry
## 1      Ashe  1091 MULTIPOLYGON (((-81.47276 3...
## 2 Alleghany   487 MULTIPOLYGON (((-81.23989 3...
## 3     Surry  3188 MULTIPOLYGON (((-80.45634 3...
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggsf <-
  ggplot(nc) +
  geom_sf(aes(fill = BIR74, label = NAME)) +
  scale_fill_viridis_c()
## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown aesthetics: label
ggplotly(ggsf)
info <- paste0(
  "\nname: ", nc$NAME,
  "\narea:", nc$AREA,
  "\nbirths in 1974:", nc$BIR74,
  "\nSIDS cases in 1974:", nc$SID74)

info[1:2]
## [1] "\nname: Ashe\narea:0.114\nbirths in 1974:1091\nSIDS cases in 1974:1"    
## [2] "\nname: Alleghany\narea:0.061\nbirths in 1974:487\nSIDS cases in 1974:0"
nc$info <- info
ggsf <-
  ggplot(nc) +
  geom_sf(aes(fill = BIR74, label = info)) +
  scale_fill_viridis_c()
## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown aesthetics: label
ggplotly(ggsf,tooltip = "label")
plot_ly(nc,
        color = ~BIR74,
        split = ~info,
        showlegend = FALSE,
        alpha = 1,
        type = "scatter",
        mode = "lines",
        hoverinfo = "text") |>
  colorbar(title = "Births in 1974")