简体   繁体   中英

Plotly way to add layers to map instead of GGplotly

I am trying to create a choropleth with a color variable for the area, and a point variable that will vary in size depending on numerical value. The variables used here are not the final data but purely for illustration.

I have used the package absmapsdata to create the type of plot I want with ggplotly. However I am unable to make it work in Plotly. I would much prefer to use plotly.

I would like to put in place a color layer and a point layer with plotly using this mapping data (or any choropleth data with geometry feature).

Here is what I have tried so far:

With ggplotly

remotes::install_github("wfmackey/absmapsdata")

library(tidyverse)
library(sf)
library(absmapsdata)
library(plotly)

mapdata <- sa32016

glimpse(mapdata)

gives

Rows: 358
Columns: 12
$ sa3_code_2016   <chr> "10102", "10103", "10104", "10105", "10106", "10201", "1…
$ sa3_name_2016   <chr> "Queanbeyan", "Snowy Mountains", "South Coast", "Goulbur…
$ sa4_code_2016   <chr> "101", "101", "101", "101", "101", "102", "102", "103", …
$ sa4_name_2016   <chr> "Capital Region", "Capital Region", "Capital Region", "C…
$ gcc_code_2016   <chr> "1RNSW", "1RNSW", "1RNSW", "1RNSW", "1RNSW", "1GSYD", "1…
$ gcc_name_2016   <chr> "Rest of NSW", "Rest of NSW", "Rest of NSW", "Rest of NS…
$ state_code_2016 <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "…
$ state_name_2016 <chr> "New South Wales", "New South Wales", "New South Wales",…
$ areasqkm_2016   <dbl> 6511.1906, 14283.4221, 9864.8680, 9099.9086, 12136.1738,…
$ cent_long       <dbl> 149.6013, 148.9415, 149.8063, 149.6054, 148.6799, 151.21…
$ cent_lat        <dbl> -35.44939, -36.43952, -36.49933, -34.51814, -34.58077, -…
$ geometry        <MULTIPOLYGON [°]> MULTIPOLYGON (((149.979 -35..., MULTIPOLYGO…

The interactive map is plotted with ggplotly as follows:

fig1 <- mapdata %>%
  filter(gcc_name_2016 == "Greater Melbourne") %>%   
  ggplot(aes(text = paste("Area:", sa3_name_2016, "<br>","Size:", areasqkm_2016))) +
  geom_sf(aes(geometry = geometry))+
  geom_point(aes(cent_long, cent_lat, size = areasqkm_2016))

fig1.plot <- ggplotly(fig1 , tooltip = "text")

fig1.plot

giving

在此处输入图像描述

I tried to put in a polygon layer and a point layer with plotly but have not had much luck

fig2.plot <- mapdata %>%
  filter(gcc_name_2016 == "Greater Melbourne") %>%
  plot_geo(split = ~sa3_name_2016, showlegend = FALSE, hoverinfo = "text",
           text = ~paste("Area:", sa3_name_2016, "<br>","Size:", areasqkm_2016)) %>%
  add_markers(x = ~cent_long, y = ~cent_lat,  size = ~areasqkm_2016)%>% 
  layout(showlegend = FALSE)
 

fig2.plot

giving

在此处输入图像描述

When I drop the add_markers() layer it looks better but I get some strange warnings

fig2a.plot <- mapdata %>%
  filter(gcc_name_2016 == "Greater Melbourne") %>%
  plot_geo(split = ~sa3_name_2016, showlegend = FALSE, hoverinfo = "text",
           text = ~paste("Area:", sa3_name_2016, "<br>","Size:", areasqkm_2016)) %>%
  layout(showlegend = FALSE)


fig2a.plot

giving

在此处输入图像描述

And the following warning

Warning message:
The trace types 'scattermapbox' and 'scattergeo' require a projected coordinate system that is based on the WGS84 datum (EPSG:4326), but the crs provided is: '+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs '. Attempting transformation to the target coordinate system. 

Basically I want to use shapefiles to make figure 2a with centroid data for points, but without the strange lines and errors, with plotly

The solution was to add_sf() to the plot_geo() code:

mapdata %>%
    filter(gcc_name_2016 == "Greater Melbourne") %>%
    plot_geo(split = ~sa3_name_2016, showlegend = FALSE, hoverinfo = "text",
             text = ~paste("Area:", sa3_name_2016, "<br>","Size:", areasqkm_2016)) %>%
    add_sf() %>%
    add_markers(x = ~cent_long, y = ~cent_lat,  size = ~areasqkm_2016)%>%
    layout(showlegend = FALSE)

I was advised that, similar to ggplot(), plot_ly() and plot_geo() are for initialization and "global" mappings. They don't actually create a plot layer until you start add_*()ing traces.

What @monkeyshines figured out worked for me. However I also wanted to add an "Open street maps" topographical layer. This worked for me.

  # districts is a shapefile that has been read, and is an SF object
  # facilities: a CSV with a latitude and longitude column
  districts %>% plot_mapbox() %>% add_sf(
  ) %>% add_markers(
    data=facilities,
    y = ~latitude, 
    x = ~longitude
  ) %>% layout(
    mapbox = list(
      zoom = 4,
      style = 'open-street-map'))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM