0
votes

Trying to plot some data in R - I am a basic user and teaching myself. However, whenever I try to plot, it fails, and I am not sure why.

> 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

This is my code, but I thought it might be an issue with my .csv file? I have attributed numbers to the "Type_ID" and "Gender_ID" columns. Type_ID has values between 1-20; Gender_ID has 1 for male, 2 for female, and 3 for both. I should state that both ID columns are just made of numeric values. Nothing more.

I then tried using barplot function. This error occurred:

> 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

There are no missing values, no characters within these columns, nothing that SHOULD cause an error according to my basic knowledge. I am just not sure what is going wrong.

2

2 Answers

0
votes

To me it seems as you are giving the plot function the wrong inputs. For the x and y axis plot expects numeric values and you are only providing a single string. The function does not know that the "Type_ID" and "Gender_ID" come from the Pokemon_BST data frame.

To reach your data you must tell R where the object comes from. You do this by opening square brackets behind the object you want to access and write the names of the objects to be accessed into it.

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"])

See also here for a introduction on subsetting in R

0
votes

The problem is how you're passing the values to the plot function. In your code above, "Gender_ID" is just some string and the plot function doesn't know what to do with that. One way to plot your values is to pass the vectors Pokemon_BST$Gender_ID and Pokemon_BST$Type_ID to the function.

Here's a sample dataframe with the plot you were intending.


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)