【问题标题】:Why do I get an error message plotting indicating infinite values when there are none?为什么在没有值时我会收到一条错误消息,指示无限值?
【发布时间】:2021-01-19 18:40:54
【问题描述】:

这是代码(以前可以工作):

require('RCurl')
require(repr) 
require(RCurl)
require(foreign)
require(tidyverse) 

states = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv", sep =",",header = TRUE)
states <- states %>%
  mutate(date = as.POSIXct(date, format = '%Y-%m-%d'))

last_day <- states[states$date==states$date[nrow(states)],]

with(last_day, plot(state, cases, las=2, cex.axis=.9, cex.main=1.5,
                    main="Confirmed cases by state",
                    xlab="", ylab=""))

...这是错误消息:

plot.window(...) 中的错误:需要有限的“xlim”值 另外:警告信息: 1:在 xy.coords(x, y, xlabel, ylabel, log) 中:强制引入的 NA 2:在 min(x) 中:min 没有非缺失参数;返回 Inf 3:在 max(x) 中:max 没有非缺失参数;返回 -Inf

但是不需要对数绘图;并且没有无限的价值:

> summary(last_day$cases)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     70   23562   86525  134731  154429  831494 

【问题讨论】:

  • 您需要barplot 吗?即barplot(with(last_day, tapply(cases, state, FUN = sum)))

标签: r plot


【解决方案1】:

我能想到的唯一原因是,如果您从隐式 stringsAsFactors=TRUE 更改为 stringsAsFactors=FALSE(例如从 R 3.x 更新到 4.x,或更改全局选项设置)。

这行得通(情节丑陋,但行得通):

with(last_day,plot(factor(state),cases))

这是我对ggplot(几乎)单行的建议:

ggplot(last_day, aes(x = reorder(factor(state),cases), y = cases)) + 
         geom_col() + coord_flip() + labs(x="",y="confirmed cases")

【讨论】:

    【解决方案2】:

    如果我们需要barplot,那就更简单了

    barplot(with(last_day, tapply(cases, state, FUN = sum)))
    

    ggplot

    library(ggplot2)
    ggplot(last_day, aes(x = state, y = cases, fill = state)) + 
          geom_col()
    

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2021-03-11
      • 2015-01-25
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多