R Plotly是一种将图层添加到地图而不是GGplotly的方法

R Plotly是一种将图层添加到地图而不是GGplotly的方法,r,plotly,choropleth,ggplotly,R,Plotly,Choropleth,Ggplotly,我正在尝试创建一个choropleth,其中包含一个区域颜色变量和一个根据数值大小变化的点变量。此处使用的变量不是最终数据,仅用于说明 我已经使用包absmapsdata创建了我想要的ggplotly类型的绘图。然而,我无法使它在计划中工作。我更喜欢用情节性的语言 我想放置一个颜色层和一个点层,并使用该映射数据(或任何具有几何特征的choropleth数据)进行绘图 以下是我迄今为止所尝试的: 含情脉脉 remotes::install_github("wfmackey/absmaps

我正在尝试创建一个choropleth,其中包含一个区域颜色变量和一个根据数值大小变化的点变量。此处使用的变量不是最终数据,仅用于说明

我已经使用包absmapsdata创建了我想要的ggplotly类型的绘图。然而,我无法使它在计划中工作。我更喜欢用情节性的语言

我想放置一个颜色层和一个点层,并使用该映射数据(或任何具有几何特征的choropleth数据)进行绘图

以下是我迄今为止所尝试的:

含情脉脉

remotes::install_github("wfmackey/absmapsdata")

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

mapdata <- sa32016

glimpse(mapdata)

基本上,我想使用Shapefile创建图2a,图2a中有点的质心数据,但没有奇怪的线和错误,plotly的解决方案是
将_sf()
添加到
plot_geo()
代码:

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)
mapdata%>%
过滤器(gcc_name_2016==“大墨尔本”)%>%
plot_geo(split=~sa3_name_2016,showlegend=FALSE,hoverinfo=“text”,
text=~粘贴(“面积:”,sa3_名称_2016,
“,“大小:”,面积平方公里(u 2016))%>% 添加_sf()%>% 添加标记(x=~cent\u长,y=~cent\u宽,大小=~areasqkm\u 2016)%>% 布局(showlegend=FALSE)

我被告知,与ggplot()类似,plot_ly()和plot_geo()用于初始化和“全局”映射。直到你开始添加痕迹,他们才真正创建绘图层。

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

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