0

(帖子已被重新设计以更加清晰,并更新了所有链接)

最近了解了dtplyr包,因此试图通过使用lazy_dt()而不仅仅是dplyr来使用它们来使我现有的代码更快地工作。

但是当我运行它们时出现错误:

第一步:数据:

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

(更新正确的形状文件) Step1.2:从以下位置下载形状文件: https ://github.com/johnsnow09/covid19-df_stack-code/blob/main/in_countries.shp

step2:转换为lazy_dt()

# creating new dataset to keep difference in both datasets
vaccination_data2 <- lazy_dt(vaccination_data)

第3 步:绘制lazy_dt()ievaccination_data2不起作用,因为绘制通常的数据框/tibble ievaccination_data起作用

# using lazy_dt data
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")

给出错误:CPL_transform 中的错误(x,crs,aoi,管道,reverse,desired_accuracy,:与 STRSXP 不兼容:[type=NULL]。

下面的代码与vaccination_data作品

# using usual data frame / 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")

在此处输入图像描述

4

0 回答 0