[尝试在R中绘制随时间变化的计数时为什么会收到一条指向Inf值的错误消息?

问题描述 投票:0回答:1

我正在使用this answer中给出的代码来生成此图

library(rvest)

cachedir <- "cache"
if (!dir.exists(cachedir)) dir.create(cachedir)

URL <- "https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_daily_reports"

html <- read_html(URL)
csvlinks <- html_nodes(html, "td span") %>%
  html_nodes("a") %>%
  html_attr("href") %>%
  grep("csv$", ., value = TRUE) %>%
  paste0("https://raw.githubusercontent.com", .) %>%
  gsub("/blob", "", .)
csvfiles <- file.path(cachedir, basename(csvlinks))
donothave <- !file.exists(csvfiles)
csvlinks <- csvlinks[donothave]
csvfiles <- csvfiles[donothave]


ign <- Map(function(l,f) download.file(l, f, quiet=TRUE), csvlinks, csvfiles)

csvfiles2 <- list.files(path = cachedir, pattern = "csv$", full.names = TRUE)


list_of_frames <- lapply(csvfiles2, read.csv, stringsAsFactors = FALSE)

list_of_frames2 <- lapply(list_of_frames, function(x) {
  colnames(x) <- gsub(".*\\.", "", colnames(x))
  x
})

renamer <- c(
  State = "Province_State",
  Region = "Country_Region",
  Update = "Last_Update",
  Latitude = "Lat",
  Longitude = "Long_"
)
list_of_frames3 <- lapply(list_of_frames2, function(x) {
  nms <- colnames(x)
  colnames(x) <- ifelse(nms %in% names(renamer), renamer[ nms ], nms)
  x
})


alldata <- data.table::rbindlist(list_of_frames3, fill = TRUE)

fmts <- c("%m/%d/%y %H:%M", "%m/%d/%Y %H:%M", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S")
timestamp <- rep(Sys.time()[NA], nrow(alldata))
for (fmt in fmts) {
  if (!any(isna <- is.na(timestamp))) next
  timestamp[isna] <- as.POSIXct(alldata$Last_Update[isna], format = fmt)
}

alldata$Last_Update <- timestamp


Atlantic <- alldata[alldata$Admin2=="Atlantic",]
Atlantic[,Atlantic$Confirmed]
#[1]  5  6  6 12 10 14 17 24 29
Atlantic[,Atlantic$Last_Update]
#[1] "2020-03-22 23:45:00 EDT" "2020-03-23 23:19:34 EDT"
#[3] "2020-03-24 23:37:31 EDT" "2020-03-25 23:33:19 EDT"
#[5] "2020-03-26 23:48:35 EDT" "2020-03-27 22:14:55 EDT"
#[7] "2020-03-28 23:05:37 EDT" "2020-03-29 23:08:25 EDT"
#[9] "2020-03-30 22:52:45 EDT"
plot("Confirmed", "Last_update", Atlantic, xaxt='n')
#Error in plot.window(...) : need finite 'xlim' values
#In addition: Warning messages:
#1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
#2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
#3: In min(x) : no non-missing arguments to min; returning Inf
#4: In max(x) : no non-missing arguments to max; returning -Inf
#5: In min(x) : no non-missing arguments to min; returning Inf
#6: In max(x) : no non-missing arguments to max; returning -Inf
axis.Date(1,at=alldata$Last_Update,labels=format(alldata$Last_Update,"%y-m-%d"),las=2)

我试图修改时间格式的结构无济于事。

r plot time-series
1个回答
0
投票

使用此行,您正在调用基本R图

plot("Confirmed", "Last_update", Atlantic, xaxt='n')

并绘制一个角色与另一个角色,这是行不通的。所以最有可能您需要这样的东西:

with(as.data.frame(Atlantic),plot(Last_Update,Confirmed,xaxt="n"))
axis.POSIXct(1,at=Atlantic$Last_Update,
labels=format(Atlantic$Last_Update,"%y-%m-%d"),las=2)

enter image description here

相关问题

为什么我在尝试使用 fsolve 查找函数的解决方案时收到错误消息?

如何在 R 中对 SPSS .sav 文件运行统计信息?

在融化的时间序列中没有得到 jco 调色板

在RStudio中导入数据库错误(意外的'\')

以栅格格式放置模型预测时出现错误消息

如何为基于日历的热图(calmap)创建自定义颜色图例?

使用 file.path() 时无法识别我的文件目录

错误:“name_id”列无法自动转换为整数类型您可能需要指定“USING name_id::integer”

第一项为“ID”的 CSV 文件在 Excel 中已损坏

ggplot:比例填充手册不会改变列的颜色

在泰坦尼克号数据集上使用 ggplot 时,我收到错误“计算美学问题。发生错误...找不到对象

条件表达式:错误消息说不完整的类型

为什么一直出现这个错误?

dplyr 尝试进行条件类型转换时出现“promise already under evaluation”错误

sparklyr- JAVA_HOME 已设置但未指向有效版本

连接点在 R 绘图动画中消失

为什么我会收到错误 - TeleBot:“无限轮询异常:'list' 对象没有属性 'id' 特别是当我使用 new_chat_members 时?

我的数据中没有 NA 或 INF 值,但是当我运行双向方差分析时。它引发了一个错误:ValueError: array must not contain infs or NaNs

计算<< with char* argument prints string, not pointer value

Visual Studio 问题 - 无法创建 SQL Server 数据库项目

热门问答
最新问题