why are ggplot output files no longer appearing?

拟墨画扇 提交于 2020-05-16 07:49:45

问题


With lots of help from this forum, I had been generating animated maps of covid-19 data for the past couple of days for work. Minus a number of overlays that my office has requested, the basic script I was using is

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)

# Get COVID cases, available from:
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"))

# Obtain map data for counties (to link with covid data) and states (for showing borders)
states_sf <- get_urbn_map(map = "states", sf = TRUE)
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)

# Merge county map with total cases of cov
counties_cov <- inner_join(counties_sf, Covid, by=c("county_fips"="countyFIPS"))

setwd("C:/mypath")
p <- 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='green', high='red',
                      na.value = "white",
                      breaks=c(1, max(counties_cov$cases))) +
  theme_bw() + 
    theme(legend.position="bottom", 
        panel.border = element_blank(),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank())

print(p + transition_time(infected) + 
        labs(title='Confirmed COVID-19 Cases: {frame_time}'))

png_files <- list.files("C:/mypath", pattern = ".*png$", full.names = TRUE)
st = format(Sys.time(), "%Y-%m-%d")
gifName <- paste("COVID-19-Cases-byCounty_",st,".gif",sep="")
gifski(png_files, gif_file = gifName, width = 800, height = 600, delay = 0.25, loop=FALSE)

This had been working great. It takes about 30 minutes on my laptop, but when it was finished I had ~100 .png files representing each day's data and the animated gif that stitched them all together sitting in the working directory.

Earlier today I updated packages...didn't pay much attention, just used RStudio package manager to check for updates and said update all.

First I got an error

Error: stat_sf requires the following missing aesthetics: geometry

I believe I fixed that by changing

geom_sf(mapping = aes(fill = cases), color = NA) +

to

geom_sf(mapping = aes(fill = cases, geometry=geometry), color = NA) +

Now the script runs, I get the rendering progress bar like I've seen before. There's even an animated map sitting in the RStudio viewer But no .png files appear in the working directory, and so gifski has nothing to build a gif from.

What do I need to do to get the output files back? This is a) making me feel very stupid and b) keeping me from moving on to other work...

Thanks!


回答1:


Instead of simply printing you have to save the frames of your animation. Try this:

a <- p + transition_time(infected) + 
            labs(title='Confirmed COVID-19 Cases: {frame_time}')

animate(a, nframes = 24, device = "png", renderer = file_renderer("C:/mypath", prefix = "gganim_plot", overwrite = TRUE))

With argument nframes you can set the number of frames, with prefix you can set the prefix of the png-files.



来源:https://stackoverflow.com/questions/60893598/why-are-ggplot-output-files-no-longer-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!