Skip to content

plotly not plotting sf multipolygon objects #1659

Closed
@erstearns

Description

@erstearns

I am trying to use plotly to create interactive maps using a multipolygon sf object as the input. I am able to get a plotly map using one dataset, and when I compare to the dataset I am interested in, they appear to be of the same class, however I cannot get my dataset working. When I try to plot with the data of interest I get the following error, despite the object actually being a multipolygon sf object:

Error in st_coordinates.sfc(model$geometry) : not implemented for objects of class sfc_GEOMETRYCOLLECTION

Environment:

  • R version: 3.6.1
  • Platform: x86_64-pc-linux-gnu (OS: Ubuntu 18.04.3 LTS)
  • plotly version: plotly_4.9.0.9000 (dev version)
  • sf version: sf_0.8-0

Below is a reproducible example that results in the error above:

library(plotly)
library(sf)
library(spData)
plot_ly(world, split = ~iso_a2, color = ~pop)

Below is a reproducible example that runs successfully:

library(plotly)
library(sf)
library(spData)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
plot_ly(nc, split=~NAME)

When I examine each object, I get the following output:
class(st_geometry(world))

[1] "sfc_MULTIPOLYGON" "sfc"

class(st_geometry(nc))

[1] "sfc_MULTIPOLYGON" "sfc"

I have tried running the following, to investigate the error message and get the expected matrix of coordinates without error:

world_coords <- st_coordinates(world)
head(world_coords)
        X         Y L1 L2 L3

[1,] 180.0000 -16.06713 1 1 1
[2,] 180.0000 -16.55522 1 1 1
[3,] 179.3641 -16.80135 1 1 1
[4,] 178.7251 -17.01204 1 1 1
[5,] 178.5968 -16.63915 1 1 1
[6,] 179.0966 -16.43398 1 1 1

nc_coords <- st_coordinates(nc)
head(nc_coords)
         X        Y L1 L2 L3

[1,] -81.47276 36.23436 1 1 1
[2,] -81.54084 36.27251 1 1 1
[3,] -81.56198 36.27359 1 1 1
[4,] -81.63306 36.34069 1 1 1
[5,] -81.74107 36.39178 1 1 1
[6,] -81.69828 36.47178 1 1 1

It is unclear to me what the issue is with the world dataset, what is different between the world and the nc dataset, and what is unique to the nc dataset that enables plotly plotting. I am able to plot with tmap, thus I do not believe the object itself to be the issue. Any help with this issue is greatly appreciated.

tm_shape(world) + tm_polygons()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions