【问题标题】:Plotting data in R: Error in plot.window(...) : need finite 'xlim' values在 R 中绘制数据:plot.window(...) 中的错误:需要有限的“xlim”值
【发布时间】:2021-03-28 08:17:43
【问题描述】:

尝试在 R 中绘制一些数据 - 我是一个基本用户并自学。但是,每当我尝试绘图时,它都会失败,我不知道为什么。

> View(Pokemon_BST)
> Pokemon_BST <- read.csv("~/Documents/Pokemon/Pokemon_BST.csv")
>   View(Pokemon_BST)
> plot("Type_ID", "Gender_ID")

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

这是我的代码,但我认为这可能是我的 .csv 文件的问题?我已将数字归因于“Type_ID”和“Gender_ID”列。 Type_ID 的值介于 1-20 之间; Gender_ID 男性为 1,女性为 2,两者均为 3。我应该声明这两个 ID 列都是由数值组成的。仅此而已。

然后我尝试使用 barplot 函数。发生此错误:

> barplot("Gender_ID", "Type_ID")

Error in width/2 : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(width) : argument is not numeric or logical: returning NA

没有缺失值,这些列中没有字符,根据我的基本知识,没有任何内容应该导致错误。我只是不确定出了什么问题。

【问题讨论】:

    标签: r ggplot2 plot bar-chart


    【解决方案1】:

    在我看来,您给plot 函数提供了错误的输入。 对于 x 和 y 轴 plot 需要数值,您只提供一个字符串。该函数不知道"Type_ID""Gender_ID"来自Pokemon_BST数据框。

    要获取您的数据,您必须告诉R 对象来自何处。为此,您可以在要访问的对象后面打开方括号,然后将要访问的对象的名称写入其中。

    View(Pokemon_BST)
    Pokemon_BST <- read.csv("~/Documents/Pokemon/Pokemon_BST.csv")
    # Refer to the object 
    plot(Pokemon_BST["Type_ID"], Pokemon_BST["Gender_ID"])
    
    # Sould also work now 
    barplot(Pokemon_BST["Gender_ID"], Pokemon_BST["Type_ID"])
    

    另见hereR中关于子集的介绍

    【讨论】:

    • 感谢您的帮助。不幸的是,这似乎没有成功... plot 函数出现此错误:Error in stripchart.default(x1, ...) : invalid plotting method barplot 函数出现此错误:Error in barplot.default(Pokemon_BST["Gender_ID"], Pokemon_BST["Type_ID"]) : 'height' must be a vector or a matrix In addition: Warning message: In mean.default(width) : argument is not numeric or logical: returning NA
    • Pokemon_BST["Gender_ID"] 是一个列表。我们想要一个绘图函数的向量。向量是 Pokemon_BST[,"Gender_ID"] 代码应该是:plot(Pokemon_BST[,"Type_ID"], Pokemon_BST[,"Gender_ID"])
    【解决方案2】:

    问题在于您如何将值传递给绘图函数。在上面的代码中,“Gender_ID”只是一些字符串,绘图函数不知道如何处理它。绘制值的一种方法是将向量 Pokemon_BST$Gender_ID 和 Pokemon_BST$Type_ID 传递给函数。

    这是一个示例数据框,其中包含您想要的情节。

    
    Pokemon_BST <- data.frame(
      Type_ID = sample(1:20, 10, replace = TRUE),
      Gender_ID = sample(1:3, 10, replace = TRUE))
    
    plot(Pokemon_BST$Gender_ID, Pokemon_BST$Type_ID)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多