首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在R中绘制数据: plot.window(...)中的错误:需要有限的'xlim‘值

在R中绘制数据: plot.window(...)中的错误:需要有限的'xlim‘值
EN

Stack Overflow用户
提问于 2021-03-28 16:17:43
回答 2查看 105关注 0票数 0

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

代码语言:javascript
运行
复制
> 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,男性为1,女性为2,两者均为3。我应该声明,这两个ID列都是由数值组成的。没别的了。

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

代码语言:javascript
运行
复制
> 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

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

EN

回答 2

Stack Overflow用户

发布于 2021-03-28 16:49:36

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

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

代码语言:javascript
运行
复制
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"])

有关R中子设置的介绍,另请参见here

票数 0
EN

Stack Overflow用户

发布于 2021-03-29 01:18:09

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

这是一个示例数据框,其中包含您想要的曲线图。

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66839544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档