将图层添加到地图而不是 GGplotly 的 Plotly 方法
Plotly way to add layers to map instead of GGplotly
我正在尝试创建一个带有区域颜色变量的等值线,以及一个根据数值大小而变化的点变量。此处使用的变量并非最终数据,仅供参考。
我已经使用包 absmapsdata 通过 ggplotly 创建了我想要的绘图类型。但是我无法让它在 Plotly 中工作。我更愿意使用 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.
基本上我想使用 shapefile 制作图 2a,其中包含点的质心数据,但没有奇怪的线条和错误,plotly
解决方案是 add_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)
有人告诉我,与 ggplot() 类似,plot_ly() 和 plot_geo() 用于初始化和“全局”映射。在您开始 add_*()ing traces 之前,它们实际上不会创建绘图层。
@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'))
我正在尝试创建一个带有区域颜色变量的等值线,以及一个根据数值大小而变化的点变量。此处使用的变量并非最终数据,仅供参考。
我已经使用包 absmapsdata 通过 ggplotly 创建了我想要的绘图类型。但是我无法让它在 Plotly 中工作。我更愿意使用 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.
基本上我想使用 shapefile 制作图 2a,其中包含点的质心数据,但没有奇怪的线条和错误,plotly
解决方案是 add_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)
有人告诉我,与 ggplot() 类似,plot_ly() 和 plot_geo() 用于初始化和“全局”映射。在您开始 add_*()ing traces 之前,它们实际上不会创建绘图层。
@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'))