gpt4 book ai didi

r - 将图层添加到 map 而不是 GGplotly 的 Plotly 方法

转载 作者:行者123 更新时间:2023-12-04 15:20:55 24 4
gpt4 key购买 nike

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

我已经使用包 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…

交互式 map 用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

给予

enter image description here

我尝试用 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

给予

enter image description here

当我放下 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

给予

enter image description here

还有以下警告

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() 用于初始化和“全局”映射。在您开始添加 _*() 跟踪之前,它们实际上不会创建绘图层。

关于r - 将图层添加到 map 而不是 GGplotly 的 Plotly 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63322502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com