[尝试在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

© www.soinside.com 2019 - 2024. All rights reserved.