I apologize for not generating pseudo data for this question, but I think the problems I am facing are basic to most non novice individuals on this site. I am attempting to create a loop that plots a scatterplot of x and y for each value of a z variable.
x=rnorm(n=50)
y=rnorm(n=50)
z<-rep(c(1,2,3,4,5),10)
dataset <-cbind(x,y,z)
Dataset<-as.data.frame(dataset)
attach(Dataset)
jpeg()
z <-Dataset$z[1:5]
for(i in 1:5) {
y<-y[z==i]
x <-x[z==i]
ARMAXpath<-file.path("C:", "Desktop", paste("myplot_", z[i], ".jpg", sep=""))
jpeg(file = ARMAXpath)
TheTitle = paste("Scatter Plots", z[i])
plot.new()
plot.window(xlim=c(0,1), ylim=c(5,10))
plot(y,x)
dev.off()
}
detach(Dataset)
No matter what I do I get the same plot.window error. I ran this code with and without attach. I ran it with and without plot.window. I also moved it in and outside the loop.
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
My question is how do I generate plots of two time series by a third variable in my dataset (i.e. region) write the output to a file folder as I have poorly attempted to do above?
Why not: consolidate all three plot calls into:
plot(y,x, xlim=c(0,1), ylim=c(5,10))
Some alternative code, using #DWin's comment:
x=rnorm(n=50)
y=rnorm(n=50)
z<-rep(c(1,2,3,4,5),10)
Dataset<-data.frame(x=x,y=y,z=z)
my.plot <- function(x,y,z){
ARMAXpath<-file.path("C:", "Desktop", paste0("myplot_", z, ".jpg"))
jpeg(file = ARMAXpath)
plot(y,x, xlim=c(0,1), ylim=c(5,10))
dev.off()
}
by(Dataset, Dataset$z, function(d) my.plot(d$x,d$y,unique(d$z)))
Related
I am trying to use nnetar for some time series forecasting, and running into an issue when the data has repeating values (i.e. the same counts observed in a time period). To reproduce the error I have created a list of values and replaced the first 10 values with a 0:
dummy.ls <- runif(n=80)
for(i in 1:10)
dummy.ls[i] <- 0
fit <- nnetar(dummy.ls, lambda=0)
When running the nnetar function I receive the following error:
Error in approx(idx, x[idx], tt, rule = 2) :
need at least two non-NA values to interpolate
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
I see similar errors in other questions, but unsure how to avoid the error?
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)
> B<-subset(olympic,sport=="basketball")
> BM<-subset(B,sex=="M"
+ )
> boxplot(BM$height)
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs):
need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
This is what happened when I try to plot the graph. I'm new to R.
Just add ylim=c(0,300)) to your code
I see nothing wrong in the command though it can be shortened to:
> BM <- subset(olympic,sport=="basketball" & sex == 'M')
> boxplot(BM$height)
The error that you are getting might be because of the fact that data.frame BM has zero rows.
I would recommend you to please check the case of values for sport (i.e. whether in the dataset, 'Basketball' is present and you are searching for 'basketball')
I have a simple csv file containing 2 columns of numbers with the headers "Colli_On" and "Colli_Off". I have written a simple Rscript which passes 3 arguments - file name and column names - and would like to produce a Bland Altman plot. However I get the following error message
> Error in plot.window(...) : need finite 'xlim' values
Calls: baplot ... do.call -> plot -> plot.default -> localWindow -> plot.window
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Where am I going wrong?
#!/usr/bin/Rscript
# -*- mode: R =*-
#script passes 3 arguments filename and 2 columns and does bland altman analysis
#Example BA /home/moadeep/Data/sehcat.csv Colli_on Colli_off
args <- commandArgs(TRUE)
mydata <- read.csv(file=args[1],head=TRUE,sep="\t")
baplot = function(x,y){
bamean = (x+y)/2
badiff = (y-x)
plot(badiff~bamean, pch=20, xlab="mean", ylab="difference")
# in the following, the deparse(substitute(varname)) is what retrieves the
# name of the argument as data
title(main=paste("Bland-Altman plot of collimator x and y\n",
deparse(substitute(x)), "and", deparse(substitute(y)),
"standardized"), adj=".5")
#construct the reference lines on the fly: no need to save the values in new
# variable names
abline(h = c(mean(badiff), mean(badiff)+1.96 * sd(badiff),
mean(badiff)-1.96 * sd(badiff)), lty=2)
}
pdf(file="test.pdf")
baplot(mydata$args[2],mydata$argss[3])
dev.off()
The problem is with this line:
baplot(mydata$args[2],mydata$argss[3])
Let's not even mention the typo... When you ask for mydata$args[2], R looks for a column named "args" in your data.frame. Obviously, there is no such column so you get NULL. The programmatic way of extracting columns from a data.frame is using [. The correct syntax should be:
baplot(mydata[args[2]],mydata[args[3]])
That should fix your problem.
(Also note that the [ operator, unlike $, will throw an error if you are trying to extract a column that does not exist: a preferable feature IMHO.)
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.