尝试在 R 中绘制随时间变化的计数时,为什么会收到指向 Inf 值的错误消息? [英] Why do I get an error message pointing to Inf values when trying to plot counts over time in R?

查看:28
本文介绍了尝试在 R 中绘制随时间变化的计数时,为什么会收到指向 Inf 值的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

I am using the code given in this answer to generate this plot

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)

I tried modifying the structure of the time format to no avail.

解决方案

With this line, you are calling the base R plot

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

And plot a character versus another character, which is not going to work. So most likely you need something like this:

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)

这篇关于尝试在 R 中绘制随时间变化的计数时,为什么会收到指向 Inf 值的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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