plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.57k stars 626 forks source link

plotly not plotting sf multipolygon objects #1659

Closed erstearns closed 4 years ago

erstearns commented 5 years ago

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:

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()

NateMietk commented 4 years ago

I am running into the same problem using this tutorial (https://blog.cpsievert.me/2018/01/30/learning-improving-ggplotly-geom-sf/), and have the same versions of Plotly, sf, and R as above.

NateMietk commented 4 years ago

Out of curiosity, is there a timeline on this bug?

SebastianKrog commented 4 years ago

Hi, I have tried installing the newest version of plotly from github (4.9.2.9000) and am still seeing what i think is a related error:

library(ggplot2)
library(sf)
library(geojsonsf)
library(plotly)

dk_map <- geojson_sf("https://raw.githubusercontent.com/moestrup/covid19/master/test.json")

mainland <- ggplot(data=dk_map) +
  geom_sf()

mainland

ggplotly(mainland)

sessionInfo()

Gives the following output:

> dk_map <- geojson_sf("https://raw.githubusercontent.com/moestrup/covid19/master/test.json")
> mainland <- ggplot(data=dk_map) +
+   geom_sf()
> mainland

map

> ggplotly(mainland)
Error in st_coordinates.sfc(sf::st_geometry(model)) : 
  not implemented for objects of class sfc_GEOMETRY
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.9.2.9000 geojsonsf_1.3.3   sf_0.9-3          ggplot2_3.3.0    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4         pillar_1.4.3       compiler_3.6.3     class_7.3-15       tools_3.6.3        digest_0.6.25      viridisLite_0.3.0 
 [8] jsonlite_1.6.1     lifecycle_0.2.0    tibble_3.0.0       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.6        DBI_1.1.0         
[15] cli_2.0.2          rstudioapi_0.11    curl_4.3           e1071_1.7-3        httr_1.4.1         withr_2.1.2        dplyr_0.8.5       
[22] htmlwidgets_1.5.1  vctrs_0.3.0        classInt_0.4-3     grid_3.6.3         tidyselect_1.0.0   glue_1.3.2         data.table_1.12.8 
[29] R6_2.4.1           fansi_0.4.1        farver_2.0.3       tidyr_1.0.2        purrr_0.3.3        magrittr_1.5       scales_1.1.0      
[36] ellipsis_0.3.0     htmltools_0.4.0    units_0.6-6        assertthat_0.2.1   colorspace_1.4-1   KernSmooth_2.23-16 lazyeval_0.2.2    
[43] munsell_0.5.0      crayon_1.3.4       Cairo_1.5-12      
> 
cpsievert commented 4 years ago

@PhazeDK this is likely a different issue...would you mind creating a new issue?