rggplot2ggplotlygeom-sf

using ggplotly getting Error in sum(tickExists) : invalid 'type' (list) of argument


I've made a simple ggplot that works using the following code:

library(tigris)
library(tidyverse)
library(plotly)

states <- states(cb = TRUE, class = "sf") %>%
  mutate(GEOID = as.numeric(GEOID)) %>%
  filter(GEOID < 57) %>%
  shift_geometry()


# Create a data frame with URLs for each map
df <- data.frame(
  states = state.name,
  map1 = sample(0:1, 50, replace = TRUE),
  map2 = sample(1:3, 50, replace = TRUE),
  map3 = sample(1:2, 50, replace = TRUE),
  map1URL = paste0("http://example.com/map1_", state.abb),
  map2URL = paste0("http://example.com/map2_", state.abb),
  map3URL = paste0("http://example.com/map3_", state.abb)
)

map_df<- merge(states, df, by.x= "NAME", by.y= "states")
  
p <- ggplot(map_df) + 
  geom_sf(color = "black", aes(fill = map1, text = map1URL)) +
  coord_sf(ndiscr = FALSE) +
  theme_void() +
  theme(plot.margin = margin(0,0,0,0))
p

However when I try to generate it using ggplotly like so:

p <- ggplotly(p, tooltip = "text")

I get the following error:

Error in sum(tickExists) : invalid 'type' (list) of argument

Any help understanding whats happening here is helpful. I'm guessing it has something to do with how ggplotly reads sf data and that I need to convert it to something else. I've tried using as.data.frame() on it. The code example is completely self contained so anybody should be able to copy and run it.


Solution

  • I figured out my own question I had used another example to create my code and I left in

    coord_sf(ndiscr = FALSE) +
    

    removing that line made the whole thing work great!