R 为什么ggplot输出文件不再出现?

R 为什么ggplot输出文件不再出现?,r,ggplot2,gganimate,R,Ggplot2,Gganimate,在这个论坛的帮助下,我在过去几天的工作中一直在制作新冠病毒-19数据的动画地图。减去我办公室要求的一些叠加,我使用的基本脚本是 library(urbnmapr) # For map library(ggplot2) # For map library(dplyr) # For summarizing library(tidyr) # For reshaping library(stringr) # For padding leading zeros library(ggrepe

在这个论坛的帮助下,我在过去几天的工作中一直在制作新冠病毒-19数据的动画地图。减去我办公室要求的一些叠加,我使用的基本脚本是

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)
我相信我通过改变来解决这个问题

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

现在脚本运行了,我会像以前一样获得渲染进度条。RStudio查看器中甚至有一个动画地图,但工作目录中没有.png文件,因此gifski没有任何东西可以用来构建gif

我需要做什么才能找回输出文件?这是a)让我觉得自己很愚蠢,b)阻止我继续从事其他工作


谢谢

您必须保存动画的帧,而不是简单地打印。试试这个:

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

a这是否回答了您的问题?我一直在家里试用MicrosoftR Open,让工作人员知道我对它的看法……一定是有一个旧的gganimate,当我回到标准R并更新它时,它坏了。非常感谢!
geom_sf(mapping = aes(fill = cases, geometry=geometry), color = NA) +
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))