Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R + ggplot2 - Aggregate Data by Intervals

Tags:

r

ggplot2

I have a file where in each line i have a numeric value symbolizing an average duration:

12.3
5.4
6
...

There is some way in R to display the data in automatic or manual intervals/breaks (aggregate?). Something like this:

[0,1[ 0
[1, 6[ 1
[6, 20[ 2
...

Also, next i want to produce a plot in ggplot2 showing this data. Could i use these intervals as labels?

like image 446
Barata Avatar asked May 24 '26 07:05

Barata


1 Answers

You can bin data with the cut() function in base R or use the Hmisc package and cut2(). There are several options on how to go about cutting and slicing your data, all of which are documented in help(cut) or help(cut2) respectively.

Once you have binned your data appropriately, plotting with ggplot becomes a trivial exercise:

library(ggplot2)
#Sample data
set.seed(1)
dat <- data.frame(x = sample(1:100, 1000, TRUE))
dat$cuts <- cut(dat$x, breaks = 5)

#Make bar chart
qplot(dat$cuts)
like image 137
Chase Avatar answered May 26 '26 19:05

Chase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!