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!