0

I am new to R and trying to create a map. I looked at these two questions Q1 Q2 which helped me form this code. However, when I run it I get some errors.

library(urbnmapr) # For map
library(ggplot2)  # For map
library(dplyr)    # For summarizing
library(tidyr)    # For reshaping
library(stringr)  # For padding leading zeros
library(ggrepel)
library(ggmap)
library(usmap)
library(gganimate)
library(magrittr)
library(gifski)

url <- "https://static.usafacts.org/public/data/covid-19/covid_confirmed_usafacts.csv"
COV <- read.csv(url, stringsAsFactors = FALSE)

Covid <- pivot_longer(COV, cols=starts_with("X"),
                      values_to="cases",
                      names_to=c("X","date_infected"),
                      names_sep="X") %>%
  mutate(infected = as.Date(date_infected, format="%m.%d.%Y"),
         countyFIPS = str_pad(as.character(countyFIPS), 5, pad="0"))

states_sf <- get_urbn_map(map = "states", sf = TRUE)
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)

counties_cov <- inner_join(counties_sf, group_by(Covid, countyFIPS) %>%
                             summarise(cases=sum(cases)), by=c("county_fips"="countyFIPS"))

counties_cov %>%
  ggplot() +
  geom_sf(mapping = aes(fill = cases), color = NA) +
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "Cases", trans = "log", low='pink', high='navyblue', 
                      na.value="white", breaks=c(1, max(counties_cov$cases))) +
  theme_bw() + theme(legend.position="bottom", panel.border = element_blank())

counties_cov <- inner_join(counties_sf, Covid, by=c("county_fips"="countyFIPS")) ##WHEN I RUN THIS IT TAKES FOREVER TO LOAD AND I GET THE LITTLE RED STOP CIRCLE BUT THEN IT PROCESSES, NOT SURE IF THIS IS NORMAL

p <- ggplot(counties_cov) +
  geom_sf(mapping = aes(fill = cases), color = NA) +
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "Cases", trans = "log", low='pink', high='navyblue', 
                      na.value="white", breaks=c(1, max(counties_cov$cases))) +
  theme_bw() + theme(legend.position="bottom", panel.border = element_blank())

p <- p + transition_time(date_infected) +
  labs(title = 'Date: {frame_time}')

animate(p, end_pause=30) #WHEN I RUN THIS I GET MY ERRORS

When I run the animate line, I get this error

Error: time data must either be integer, numeric, POSIXct, Date, difftime, orhms
In addition: Warning message:
In min(cl[cl != 0]) : no non-missing arguments to min; returning Inf

So, I decided to convert the date_infected time by editing a previous line to say

p <- p + transition_time(as.Date(Covid$date_infected, "%Y-%m-%d")) +
  labs(title = 'Date: {frame_time}')

At this point, I get these errors

Error in seq.default(range[1], range[2], length.out = nframes) : 
  'from' must be a finite number
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

Can anyone please help with this? I've been struggling for hours. Let me know if you need any more info.

Thanks!

  • For a start quick observations. (i) If you check your plotting object/dataframe, i.e. counties_cov, you will see that the date is formatted with dots, e.g. counties_cov %>% pull(date_infected) %>% head() [1] "2020.01.22" "2020.01.23" "2020.01.24" "2020.01.25" "2020.01.26" "2020.01.27". In your transition_time(as.Date...) call you "prescribe" the format to be written with dashes. This is create NAs for every date as it does not apply to what you want R to look for. Thus, use as.Date(Covid$date_infected, "%Y.%m.%d") or work with lubridate::ymd() Good luck. – Ray Apr 07 '21 at 10:13
  • @Ray thank you for pointing that out. I adjusted that now! Still getting the errors for some reason :( – Ruby Samim Apr 12 '21 at 14:12
  • I know it sounds like a weak excuse, but my working week is terrible (and I have no time to look into this "line-by-line". You are processing quite a large data frame (joins, etc. I assume this results in a million line data frame). To reduce complexity and find out where in the process your error occurs, try doing this with a smaller sample, e.g. use only 1000 or 10000 rows, is subdf <- countries_cov[1:1000,]. This allows us to nail down whether it is a scale (too many rows) or data issue causing the gganimate errors – Ray Apr 14 '21 at 06:55

0 Answers0