Last active
December 18, 2018 13:14
-
-
Save cpsievert/fa56bd89122e395803f30602b774dbe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really nice! The plot I get after copy & paste the code has a different aspect ratio, making it less useful:
Also, 28 warnings of this kind: