1

I am sorting data into bins and averaging, see this solution.

I am using the exact same solution as in the above link but fixing my data to a scatter plot instead. The code which is causing me difficulty is :

myData.class <- cut(df$xaxis, seq(0,30,length=60), include.lowest=TRUE)
mean.yaxis <- tapply(df$yaxis, myData.class, mean)
lines(mean.yaxis ~ seq(0, 30, length=60))

The call to lines is producing an error :

Error in model.frame.default(formula = mean.yaxis ~ seq(0, 30, length = 60),  : 
    variable lengths differ (found for 'seq(0, 30, length = 60)')

A call to str(mean.yaxis) produces :

num [1:59(1d)] 0 0 0 0.349 4.652 ...
- attr(*, "dimnames")=List of 1
  ..$ : chr [1:59] "[0,0.508]" "(0.508,1.02]" "(1.02,1.53]" "(1.53,2.03]" ...

How can I access to correct data in my call to the function lines(...) ?

Community
  • 1
  • 1
klonq
  • 3,535
  • 4
  • 36
  • 58

2 Answers2

2

The best solution is given in the accepted answer from your link. This will plot the intervals on the x-axis.

cut returns a factor with 1 level less than your sequence (as you've seen). If you want the interval mids, you could do (taking the former example) :

data(quakes)

Seq <- c(40, 120, 200, 300, 400, 500, 600, 680)
depth.class <- cut(quakes$depth, Seq, include.lowest = TRUE)
mean.mag <- tapply(quakes$mag, depth.class, mean)

class.mids <- Seq[-1] - diff(Seq)/2

plot(mean.mag~class.mids,xlim=range(Seq))
lines(mean.mag~class.mids)

enter image description here

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
0

Maybe:

lines(mean.yaxis ~ seq(0, 30, length=length(mean.yaxis)))

HTH

Luciano Selzer
  • 9,806
  • 3
  • 42
  • 40
  • @Iselzer : this maps the means to the left border of the intervals on one side, and to the right border on the other. So basically only the middle point is right positioned, all the rest is stretched out to either the left or the right. – Joris Meys Apr 07 '11 at 17:38