首页 > 解决方案 > 使用 plotly::api_create 和 ggplot2::geom_sf 函数时文件太大

问题描述

我正在尝试使用 R 中的 plotly 在线绘制空间数据,但我收到一条错误消息“请求实体太大,此文件太大!您当前的订阅限制为 524 KB 上传。” 关于如何解决这个问题的任何线索?为了重现我的代码,您需要 (i) 在plotly上注册并 (ii) 在我的github repo上下载法语部门的 shapefile 。这 3 个文件应位于名为shapefile的文件夹中。在我看来,是ggplot2函数geom_sf会产生太大的文件。我的代码如下

require(tidyverse)
require(ggplot2)

#Info required for online plotting 
Sys.setenv("plotly_username"="replace_by_your_username")
Sys.setenv("plotly_api_key"="replace_by_your_apikey")

#Read shapefile
dep <- sf::st_read("replace_with_the_correctPATH/shapefile/DEPARTEMENT.shp")

#Variable to plot
zz<-runif(length(dep$CODE_DEPT),-10,3)

#ggplot2 object
gg <- dep %>%
mutate(discrete = cut(zz, c(-10, seq(-3, 3, by = 1)))) %>%
ggplot() +
geom_sf(aes(fill = discrete, text = paste("Department:", dep$CODE_DEPT, "<br>", "bli", zz))) +
scale_fill_brewer(palette = "PuOr", name = "bla")

#Plotting the figure on your local computer works
#plotly::ggplotly(gg, tooltip = c("text"))

#Generate an error message
plotly::api_create(gg, tooltip = c("text"),filename = "sthing")

标签: rplotlyshapefilesf

解决方案


这似乎不是你的问题,但我遇到了同样的事情。对于未来的读者,我的问题似乎是我使用的数据框(例如 100 行)是一个大于文件限制的大型数据集(15,000 行)的子集。

尽管我的子集非常小并且完全在上传限制范围内,但我必须将子集保存为 csv,将其重新加载并使用新加载的数据帧作为我的 plotly 上传。即使导入的数据框与子集数据框的行数相同,我也不得不断开与原始较大数据集的子集连接,不知道为什么。


推荐阅读