为什么ggplot输出文件不再出现? [英] why are ggplot output files no longer appearing?

查看:61
本文介绍了为什么ggplot输出文件不再出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此论坛的大力帮助下,我过去两天的工作一直在生成covid-19数据的动画地图.减去我的办公室要求的许多叠加层,我正在使用的基本脚本是

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)

这一直很好.在笔记本电脑上大约需要30分钟,但是完成后,我有约100个.png文件表示每天的数据,而动画gif则将它们缝合在一起,位于工作目录中.

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.

今天早些时候,我更新了软件包...并没有引起太多关注,只是使用RStudio软件包管理器来检查更新并说全部更新.

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

首先我遇到了错误

Error: stat_sf requires the following missing aesthetics: geometry

我相信我可以通过更改

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

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

现在脚本运行了,我得到了渲染进度条,就像我以前看到的一样.RStudio查看器中甚至还有一张动画地图,但是工作目录中没有.png文件,因此gifski无法从中构建gif.

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.

我需要怎么做才能取回输出文件?这是a)让我感到非常愚蠢,b)阻止我继续从事其他工作...

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

谢谢!

推荐答案

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

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

使用参数 nframes 可以设置帧数,使用 prefix 可以设置png文件的前缀.

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

这篇关于为什么ggplot输出文件不再出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆