尝试在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,男性为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
没有缺失值,这些列中没有字符,根据我的基本知识,没有任何应该导致错误的东西。我只是不确定哪里出了问题。
发布于 2021-03-28 16:49:36
在我看来,您似乎给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"])
有关R
中子设置的介绍,另请参见here
发布于 2021-03-29 01:18:09
问题是如何将值传递给plot函数。在上面的代码中,"Gender_ID“只是一个字符串,plot函数不知道如何处理它。绘制您的值的一种方法是将向量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)
https://stackoverflow.com/questions/66839544
复制相似问题