plot of categorical variables with R - r

I'm a newcomer both to R and statistics in general.
I'm trying to plot the relationship of two categorical variables which are vectors of length 147.
The tables for each are:
AgeGroup
26-49 50-64 65 and over Under 26
101 16 5 25
and
Role
A/C C/H Dietician
6 8 2
Doctor Healthcare Assistant Lab
56 7 2
Nurse Pharmacist Phlebotomist
54 5 1
Radiographer Therapist
1 5
After reading an answer to a question here from 2012 I expected to get a spineplot by using this command:
plot(mydata$Role,mydata$AgeGroup,xlab="Role",ylab="Age Group")
but it returns:
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
I'm guessing that Warning message 1 is telling me that xycoords() is attempting to
coerce the categorial elements into numeric data and when it fails to do so
is returning NAs, which gives plot() nothing to work with.
In short, what am I failing to understand and how do I fix it?

I guess I might as well put my comments in an answer:
There is a plot.table method so why not try:with( mydata, plot( table(Role, AgeGroup), xlab="Role",ylab="Age Group") – BondedDust 3 hours ago
with creates a local environment where the column names are interpreted as objects, and within that environment, a 'table'-object is passed to plot. The plot function is generic (try typing methods(plot) ) so it looks up the proper method for the 'table'-classed object and does its work. To see the code for plot.table which is not exported, you need to use getAnywhere(plot.table)

Related

Plotting data in R: Error in plot.window(...) : need finite 'xlim' values

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.
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
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)

Opening .tiff gives me a plot.window() error

I created a raster and saved it as a .tif file. However, when I tried to open the .tiff, I get the following error:
> plot("20200105.tif")
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
The strange thing is, before I saved the file it worked fine.
How do I manage this error?
This make no sense
plot("20200105.tif")
You are trying to plot a character string
Instead do
library(raster)
r <- raster("20200105.tif")
# or
# b <- brick("20200105.tif")
plot(r)

Problems with kriging function in R

I have a dataframe named as kef, consisted of 512 rows, and the fields x, y (referring to coordinates) and v (refering to a certain numeric value for each cell).
I also have a map layer named as grecia.map, loaded in R through the readOGR command consisted of a polygon which represents a certain area.
While running the following command:
kriged <- kriging(kef$x, kef$y, kef$v, polygons = grecia.map, pixels=30000)
I receive the following error messages:
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
In addition: Warning messages:
1: In max(x) : no non-missing arguments to max; returning -Inf
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(y) : no non-missing arguments to max; returning -Inf
4: In min(y) : no non-missing arguments to min; returning Inf
Well, I managed to correct the issue. The problem had to do with the data. the coordinates along with the sample values were messed and apparently there was not any spatial logic with the data.
After importing a correct dataset the function worked properly so I guess that this question could be deleted.
Thank you

Hi all i am ploting a graph between in R

I am plotting a graph between 2 variable in R.
Time and city are variable in city.
like my code plot(time ~ City,table)
i am getting below error message..
could you please any one advise..
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 min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
Maybe try using the following syntax
plot(1:10,21:30)
Or, probably in your case
plot(table$time,table$City)
Good luck with it! The start is always difficult.

Basic hexbin with R?

I have results from a survey. I am trying to create a graphic displaying the relationship of two variables: "Q1" and "Q9.1". "Q1" is the independent and "Q9.1" is the dependent. Both variables have responses from like scale questions: -2,-1,0,1,2. A typical plot places the answers on top of each other - not very interesting or informative. I was thinking that hexbin would be the way to go. The data is in lpp.
I have not been able to use "Q1" and "Q9.1" for x and y. However:
> is.numeric("Q1")
[1] FALSE
q1.num <- as.numeric("Q1")
Warning message:
NAs introduced by coercion
The values for Q1 are (hundreds of instances of): -2,-1,0,1,2
How can I make a hexbin graph with this data?
Is there another graph I should consider?
Error messages so far:
Warning messages:
1: In xy.coords(x, y, xl, yl) : NAs introduced by coercion
2: In xy.coords(x, y, xl, yl) : 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
How about taking a slightly different approach? How about thinking of your responses as factors rather than numbers? You could use something like this, then, to get a potentially useful representation of your data:
# Simulate data for testing purposes
q1 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
q9 = sample(c(-2,-1,0,1,2),100,replace=TRUE)
dat = data.frame(q1=factor(q1),q9=factor(q9))
library(ggplot2)
# generate stacked barchart
ggplot(dat,aes(q1,fill=q9)) + geom_bar()
You may want to switch q1 and q9 above, depending on the view of the data that you want.
Perhaps ggplot2's stat_binhex could sort that one for you?
Also, I find scale_alpha useful for dealing with overplotting.

Resources