Search code examples
rggplot2r-plotlyggplotly

Maps not showing in the RStudio Viewer when using plotly in R


I've made a map using ggplot2 in R. I am now trying to use plotly so that I can make it more interactive (and in particular use tooltips). However, anything I try ends up with me just getting a blank viewer panel in RStudio. I am able to view other things with plotly (for e.g. a basic line chart), but not maps.

For the sake of being reproducible, I am also trying using this basic map taking code from here.

library(ggplot2)
library(plotly)

usa <- map_data("usa")
states <- map_data("state")

usa_map <- ggplot(data = states) + 
  geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") + 
  coord_fixed(1.3) +
  guides(fill=FALSE)

This plots me a map, just how I want it.

Next, so far I have tried this:

ggplotly(usa_map)

And this:

usa_map_view <- plotly_build(usa_map)
usa_map_view

But both of these just give me the blank screen, without any error messages.

Any ideas where I'm going wrong?


Solution

  • Yes. As Stephane Laurent mentioned, it can be viewed in the browser.

    This is what I got in the browser. It has all the states listed on the right(scrollable list). Plot

    You can view in browser by clicking "open in new window" in "viewer".

    Open in new window