Skip to content

Instantly share code, notes, and snippets.

@cpsievert
Last active December 18, 2018 13:14
Show Gist options
  • Save cpsievert/fa56bd89122e395803f30602b774dbe1 to your computer and use it in GitHub Desktop.
Save cpsievert/fa56bd89122e395803f30602b774dbe1 to your computer and use it in GitHub Desktop.
library(sf)
library(dplyr)
library(albersusa)
library(crosstalk)
library(plotly)
usa <- usa_sf("laea")
# st_centroid gets the center POINT of polygons
uscenter <- usa %>%
st_centroid() %>%
mutate(
x = sapply(geometry, "[[", 1),
y = sapply(geometry, "[[", 2)
) %>%
SharedData$new()
p <- ggplot() +
geom_sf(data = usa, aes(fill = pop_2010, text = paste("Population", pop_2010))) +
geom_text(
data = uscenter, alpha = 0,
aes(x, y, label = name, text = "Click to label me")
)
ggplotly(p, tooltip = "text") %>%
highlight(
persistent = TRUE,
selected = attrs_selected(
hoverinfo = "none",
textfont = list(color = "black")
)
)
@edzer
Copy link

edzer commented Jan 31, 2018

Confirmed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment