0

(Post has been reframed to get more clarity & also updated all links)

Recently got to know about dtplyr package so was trying to use them to make my existing code work faster by using lazy_dt() instead of just dplyr.

But I am getting error when I run them:

step1: data:

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

(UPDATED correct Shape file) Step1.2: Download shape file from: https://github.com/johnsnow09/covid19-df_stack-code/blob/main/in_countries.shp

step2: convert to lazy_dt()

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

Step3: plotting of lazy_dt() i.e vaccination_data2 doesn't work where as plotting of usual dataframe / tibble i.e vaccination_data works

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

Gives error: Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, : Not compatible with STRSXP: [type=NULL].

Below code with vaccination_data works

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

enter image description here

ViSa
  • 1,563
  • 8
  • 30
  • Any reason you convert your data via `lazy_dt()` for plotting? I mean this only makes sense if you want to perform some data wrangling operations. Be it as it is, to access the data and use it in ggplot2 you have to convert it back to a data.frame or tibble or data.table using as.data.frame or ... – stefan Aug 17 '21 at 13:20
  • I would be performing data wrangling after this & then will plot. This was first giving me an error in shiny and I have not framed this question correctly. Let me close this Post & then I will re post. Apologies for the mess – ViSa Aug 17 '21 at 13:25

0 Answers0