首页 > 解决方案 > 将图层添加到地图而不是 GGplotly 的 Plotly 方式

问题描述

我正在尝试为该区域创建一个带有颜色变量的等值线,以及一个大小会根据数值而变化的点变量。此处使用的变量不是最终数据,仅用于说明。

我已经使用包 absmapsdata 来创建我想要的 ggplotly 绘图类型。但是我无法让它在 Plotly 中工作。我更喜欢使用情节。

我想使用此映射数据(或任何具有几何特征的等值线数据)绘制一个颜色层和一个点层。

这是我到目前为止所尝试的:

用 ggplotly

remotes::install_github("wfmackey/absmapsdata")

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

mapdata <- sa32016

glimpse(mapdata)

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…

交互式地图使用 ggplotly 绘制如下:

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

给予

在此处输入图像描述

我试图在 plotly 中放入一个多边形层和一个点层,但运气不佳

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

给予

在此处输入图像描述

当我删除 add_markers() 层时,它看起来更好,但我收到了一些奇怪的警告

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

给予

在此处输入图像描述

以及以下警告

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. 

基本上我想使用 shapefiles 来制作图 2a,其中包含点的质心数据,但没有奇怪的线条和错误,带有 plotly

标签: rplotlychoroplethggplotly

解决方案


@monkeyshines 的发现对我有用。但是我还想添加一个“开放街道地图”地形图层。这对我有用。

  # 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'))

在此处输入图像描述


推荐阅读