R: Graphing binned data
Asked Answered
R

2

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

Rondon answered 7/4, 2011 at 12:29 Comment(4)
It's a bit hard for us to work without your data or, preferably, a small, reproducible example.Ochone
@Roman The link I posted uses the pre-installed quakes dataset.Rondon
Binning continuous data is generally considered bad form. I defer to Frank Harrell on this. biostat.mc.vanderbilt.edu/wiki/Main/CatContinuousAlehouse
@Richie Thanks for the info I'm actually following IEC61400, so just doing what I am told. But good to know.Rondon
B
0

Maybe:

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

HTH

Blackberry answered 7/4, 2011 at 13:9 Comment(1)
@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.Vociferate
V
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

Vociferate answered 7/4, 2011 at 13:13 Comment(0)
B
0

Maybe:

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

HTH

Blackberry answered 7/4, 2011 at 13:9 Comment(1)
@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.Vociferate

© 2022 - 2024 — McMap. All rights reserved.