Issue: I need an interactive choropleth map of the US that will work well on mobile (ideally pinch-zoom), has a pop-up on tap/click, and has AK and HI set below. Plotly isn't it as you have to tap the tiny zoom icon, and the usmaps package shows the map I want (AK and HI below), but I can't figure out how to make it interactive with the zoom (ggplotly = plotly = not pinch-zoom). Leaflet is the reactivity I want, but I can't figure out how to get AK and HI below. The data processing is fine, it's a "How do I do the thing?" question with interactivity. DataMaps appears to have something that'd work, but it is not compatible with the version of R I have by a long shot, and is no longer updated (as of maybe 8 years ago?).
I have tried: Troubleshooting in Plotly, troubleshooting in Leaflet, spending hours doing internet searches. I can get a static map of exactly what I want in usmap, and can map it with AK and HI separated in Leaflet, but I cannot figure out how to get a US map that is more compressed and yet still interactive in the way I want it to be (it has to have a pinch-zoom or similar because of user needs). I am wondering if maybe there's a way with maybe ggiraph?
Edit: I was asked for code, and so to offer some code and an example of the map I made that I like the design of but that does not have the needed mobile interactivity (pinch-zoom):
Map prototype: https://transformationsproject.github.io/static/widgets/map_test.html
Code (a note: I know it's messy):
tf_usa_map <- us_map(regions = "states")
# Joining so can be used
tf_current_map <- left_join(tf_usa_map, tf_airtable_state_by_year, by=c("abbr" = "state_abb"))
# Removing DC
tf_current_map <- tf_current_map %>%
filter(abbr != "DC")
# Getting total count of bills for 2024
updated_total_bills <- sum(tf_current_map$`2024`)
# Building Map ----
# Setting up geometry
geo_test <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
lakecolor = toRGB('white')
)
# Setting up border
state_border <- list(color = toRGB("#2d0016"), width = 2)
# Setting up custom color scale/breaks
custom_colors <- c("white", "#ed7fa1", "#DB0044", "#740024", "#410014")
custom_breaks <- c(0, 5, 20, 30, 60)
fig3 <- plot_geo(tf_current_map) %>%
add_trace(
z = ~tf_current_map$`2024`,
#text = ~paste(State, "has had\n", tf_current_map$`2024`, "bills introduced"),
span = I(0),
locations = ~abbr,
locationmode = 'USA-states',
colorscale = list(
list(0, custom_colors[1]),
list(custom_breaks[2] / max(custom_breaks), custom_colors[2]),
list(custom_breaks[3] / max(custom_breaks), custom_colors[3]),
list(custom_breaks[4] / max(custom_breaks), custom_colors[4]),
list(1, custom_colors[5])),
zmin = min(custom_breaks),
zmax = max(custom_breaks),
# State border colors/thickness
marker = list(line = state_border),
# To do: list min/max
colorbar = list(
tickvals = list(custom_breaks[1], custom_breaks[5]),
ticktext = list(custom_breaks[1], custom_breaks[5])),
hovertext = ~paste0(`State`),
showscale = FALSE
) %>%
colorbar(title = "Bills") %>%
layout(geo = geo_test,
font = alexandria,
title = list(
text = ~paste0(updated_total_bills,
' Anti-Trans Bills Have Been',
"<br>",
'Introduced in 2024'),
font = list(size = 20)),
margin = list(l = 50, r = 50, b = 100, t = 50),
annotations = list(
list(
text = 'Data From: Trans Formations Project, LegiScan',
font = list(size = 10),
margin = list(l = 10, r = 10, b = 10, t = 20, pad = 4),
#height = 200,
showarrow = FALSE,
xref = 'paper', x = 0.5,
yref = 'paper', y = -0.2,
xanchor = 'left', yanchor = 'top'))
)
plotly
orleaflet
code that you've tried so we can get a better idea of the kind of interactivity you're looking for? Either package can do this if fed the right data format (e.g.,sf
or GeoJSON). For instance, you could usemy_data <- tigris::states() |> tigris::shift_geometry() |> sf::st_transform(4326)
and then plot this inleaflet
withleaflet() |> addPolygons(data = my_data)
. You may not be able to add basemap tiles, however, since Alaska and Hawaii will be shifted.