[R] Scatter plot produces "'x' and 'y' lengths differ"

Daniel Malter daniel at umd.edu
Fri Jun 17 08:30:48 CEST 2011


It likely means that your x and y are differently long. That is, affect1 and
adh1scr do not contain the same number of values in that instance. That
precludes them from being plotted against each other. abline and lowess
would fail for the same reason.

x<-c(1,2,3)
y<-c(2,4)
plot(y~x)
complete.cases(x,y)

However, what is peculiar about this is that if x and y indeed have
different length, the complete.cases() function should fail (unless
specified with some override argument).

The problem is definitely NOT one with XLIM or YLIM.

HTH,
Daniel






Alspach, Steven E. wrote:
> 
> Hello,
> 
>  
> 
> I am working on a project to create some scatter plots.  I have syntax
> for 26 plots, and 22 of them display as they should.  But here, for
> example, is a sample of the command syntax I am using:
> 
>  
> 
> good <- complete.cases(affect1,adh1scr)
> 
> plot
> (jitter(affect1,2.0),jitter(adh1scr,1.0),xlim=c(1,35),ylim=c(1,35),pch=1
> 6,
> 
> main='Adherence Score by Affectiveness Level - Visit 1',
> 
> ylab='Adherence score',xlab='Affect Scale - Patient
> Survey',abline(lsfit(affect1,adh1scr)),col="red")
> 
> lines(lowess(affect1[good], adh1scr[good]), col="blue")
> 
>  
> 
> I run this and get:
> 
>  
> 
>> good <- complete.cases(affect1,adh1scr)
> 
>> plot
> (jitter(affect1,2.0),jitter(adh1scr,1.0),xlim=c(1,35),ylim=c(1,35),pch=1
> 6,
> 
> +  main='Adherence Score by Affectiveness Level - Visit 1',
> 
> + ylab='Adherence score',xlab='Affect Scale - Patient
> Survey',abline(lsfit(affect1,adh1scr)),col="red")
> 
> Error in xy.coords(x, y, xlabel, ylabel, log) : 
> 
>   'x' and 'y' lengths differ
> 
>> lines(lowess(affect1[good], adh1scr[good]), col="blue")
> 
> Error in xy.coords(x, y) : 'x' and 'y' lengths differ
> 
>  
> 
> The data is a bit skewed towards the high end, but I found other
> variables that skew even more and they produce graphs.  I've taken out
> the ABLINE and LINES options and the problem persists.  I tried changing
> the name of the AFFECT1 variable, the problem exists.  I tried changing
> the values of XLIM and YLIM (the range for the X and Y variables are
> both 1 and 35); I still get the same error.  There are enough valid
> cases to create a scatter plot (two of the plots will have 211 valid
> cases, the other two will have 163 valid cases).
> 
>  
> 
> This occurs in the middle of my command file.  So - I know that the
> variables are being read into R correctly (no error messages from R when
> I define the variables), and similar syntax for other variables works -
> it's just these 4 variables (AFFECT1, AFFECT2, INFOSC1, INFOSC2) that
> produce the "Error in xy.coords" message.
> 
>  
> 
> Any help that anyone can give regarding this would be helpful.  
> 
>  
> 
> Sincerely,
> 
>  
> 
> Steve Alspach 
> 
> Jesse Brown Veterans' Administration Hospital
> 
> Steven.alspach at va.gov
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

--
View this message in context: http://r.789695.n4.nabble.com/Scatter-plot-produces-x-and-y-lengths-differ-tp3604367p3604688.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list