You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R中使用dtplyr的lazy_dt数据结合sf包绘制geom_sf图报错如何解决

(本帖已重新梳理逻辑提升清晰度,并更新了所有链接)

我最近了解到dtplyr包,想通过lazy_dt()替代原有dplyr相关操作来提升现有代码的运行速度,但运行代码时报错,具体情况如下:

步骤1:数据准备
library(tidyverse)
library(sf)
library(lubridate)
library(scales)
library(glue)

library(dtplyr)
library(data.table)

vaccination_data <- readRDS(url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/vaccination_data.rds")) 

(已更新为正确的Shape文件)

步骤1.2:Shape文件下载地址

https://github.com/johnsnow09/covid19-df_stack-code/blob/main/in_countries.shp

步骤2:转换为lazy_dt()格式
# 新建数据集区分两种数据格式
vaccination_data2 <- lazy_dt(vaccination_data)
步骤3:报错复现

使用lazy_dt()生成的vaccination_data2绘图失败,但使用常规dataframe/tibble格式的vaccination_data可正常绘图:

# 使用lazy_dt格式数据
vaccination_data2 %>% 
            filter(date > ymd("2020-12-31")) %>% 
            mutate(month_col = lubridate::month(date, label = TRUE)) %>% 
            
            arrange(date) %>% 
            group_by(Country.Region, iso_code, month_col) %>% 
            summarise(total_vaccinations = last(total_vaccinations), 
                      .groups = "drop") %>% 
            ungroup() %>% 
                      
            full_join(sf::read_sf("in_countries.shp") %>% 
                          st_as_sf(),
                      by = c("iso_code" = "iso_a3")) %>% 
            dplyr::select(Country.Region,iso_code,month_col,total_vaccinations,geometry) %>% 
            filter(!is.na(total_vaccinations)) %>% 
            as.tibble() %>% 
            st_as_sf() %>% st_transform(crs = 4326) %>% 
  
  
  ggplot() +
            geom_sf(aes(fill = total_vaccinations), color = "white") +           
            scale_fill_gradient2_tableau(
                palette = "Sunset-Sunrise Diverging",
                na.value = "lightgrey",
                guide = "colourbar",
                labels = unit_format(unit = "M", scale = 1e-6)
            ) +
            
            theme_map() +
            facet_wrap(~month_col) +
            theme(strip.background = element_blank(),
                  strip.text = element_text(face = "bold", size = 14),
                  legend.position = "right")

报错信息:
Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, : Not compatible with STRSXP: [type=NULL].

可正常运行的参考代码

以下使用vaccination_data的代码可正常运行:

# 使用常规数据框/tibble
vaccination_data %>% 
            filter(date > ymd("2020-12-31")) %>% 
            mutate(month_col = lubridate::month(date, label = TRUE)) %>% 
            
            arrange(date) %>% 
            group_by(Country.Region, iso_code, month_col) %>% 
            summarise(total_vaccinations = last(total_vaccinations), 
                      .groups = "drop") %>% 
            ungroup() %>% 
                      
            full_join(sf::read_sf("in_countries.shp") %>% 
                          st_as_sf(),
                      by = c("iso_code" = "iso_a3")) %>% 
            dplyr::select(Country.Region,iso_code,month_col,total_vaccinations,geometry) %>% 
            filter(!is.na(total_vaccinations)) %>% 
            as.tibble() %>% 
            st_as_sf() %>% st_transform(crs = 4326) %>% 
  
  
  ggplot() +
            geom_sf(aes(fill = total_vaccinations), color = "white") +           
            scale_fill_gradient2_tableau(
                palette = "Sunset-Sunrise Diverging",
                na.value = "lightgrey",
                guide = "colourbar",
                labels = unit_format(unit = "M", scale = 1e-6)
            ) +
            
            theme_map() +
            facet_wrap(~month_col) +
            theme(strip.background = element_blank(),
                  strip.text = element_text(face = "bold", size = 14),
                  legend.position = "right")

正常运行的可视化效果图


内容的提问来源于stack exchange,提问作者ViSa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 05:27:04