Dheeraj Singh
Dheeraj Singh

Reputation: 735

Plot histogram by first sorting data and then dividing x values into bins in R

I have a dataset in a given format:

USER.ID avgfrequency
1   3   3.7821782
2   7   14.7500000
3   9   13.4761905
4   13  5.1967213
5   16  6.7812500
6   26  41.7500000
7   49  13.6666667
8   50  7.0000000
9   51  1.0000000
10  52  17.7500000
11  69  4.5000000
12  75  9.9500000
13  91  84.2000000
14  98  8.0185185
15  138 14.2000000
16  139 34.7500000
17  149 7.6666667
18  155 35.3333333
19  167 24.0000000
20  170 7.3529412
21  171 4.4210526
22  175 6.5781250
23  176 19.2857143
24  177 10.4864865
25  178 28.0000000
26  180 4.8461538
27  183 25.5000000
28  184 13.0000000
29  210 32.0000000
30  215 13.4615385
31  220 11.3611111
32  223 26.2500000

I want to first sort the dataset by avgfrequency and then I want to plot count of USER.ID's that fall under different bin categories.

I want to divide avgfrequency into different bin categories of width 10.

I am trying to sort data using:

user_avgfrequency <- user_avgfrequency[order(user_avgfrequency[,1]), ]

but getting an error.

Upvotes: 1

Views: 1189

Answers (1)

bgoldst
bgoldst

Reputation: 35314

plot

df <- data.frame(USER.ID=c(3,7,9,13,16,26,49,50,51,52,69,75,91,98,138,139,149,155,167,170,171,175,176,177,178,180,183,184,210,215,220,223), avgfrequency=c(3.7821782,14.7500000,13.4761905,5.1967213,6.7812500,41.7500000,13.6666667,7.0000000,1.0000000,17.7500000,4.5000000,9.9500000,84.2000000,8.0185185,14.2000000,34.7500000,7.6666667,35.3333333,24.0000000,7.3529412,4.4210526,6.5781250,19.2857143,10.4864865,28.0000000,4.8461538,25.5000000,13.0000000,32.0000000,13.4615385,11.3611111,26.2500000) );
breaks <- seq(0,ceiling(max(df$avgfrequency)/10)*10,10);
cols <- colorRampPalette(c('blue','green','red'))(length(breaks)-1);
hist(df$avgfrequency,breaks,col=cols,axes=F,xlab='Average Frequency',ylab='Count');
axis(1,breaks);
axis(2,0:max(tabulate(cut(df$avgfrequency,breaks))));

Upvotes: 1

Related Questions