Enrique Enrique - 3 months ago 23
R Question

How do I normalize the Y-axis of histogram between 0 and 1?

I need to normalized the Y-axis of an histogram between 0-1.

This is a part of my data.frame:

1.8064 2.2016 2.4506 2.1828 2.1171 1.9308 2.1707 2.1885
2.2310 2.2400 1.9115 2.1527 2.0934 1.7989 2.2144 2.0091
1.9248 2.2038 1.9676 1.9224 1.9502 1.7990 2.0824 2.1300
2.0095 2.0341 1.8433 1.8361 1.9958 1.8243 2.0397 2.0482
2.1143 2.2627 1.7620 1.7561 1.9490 1.9803 1.9336 2.2511
2.2377 2.5414 1.7867 1.6618 2.5090 1.8325 2.0212 2.1616
2.3476 2.1878 2.0469 1.7508 2.2969 1.7939 2.0291 2.0721
2.3534 2.0932 2.3502 1.9960 2.0710 1.9923 1.7787 1.9772
2.2607 2.1504 2.3685 2.1148 2.1961 1.7738 1.8405 2.0135
2.2411 1.9916 2.4726 2.0347 2.0751 1.7570 1.8874 1.9385
2.1913 1.8981 2.2441 2.3068 2.1198 2.1484 1.8056 1.7747
2.0842 1.8750 2.3023 2.1204 1.8972 2.1534 1.8028 1.9401
2.2105 1.9618 2.2472 1.9656 2.3098 1.9771 1.9520 1.8627
2.2863 1.9959 2.1781 1.9544 1.9281 1.9286 1.9699 2.0330
2.1987 2.0583 2.0953 2.0206 2.1148 2.3789 1.7052 1.9145
2.0513 2.0850 1.9810 2.4943 1.9120 2.2209 1.9461 2.0882
2.0049 2.0416 1.9303 2.3681 1.8974 2.0054 1.9261 1.9097
1.6882 2.1196 1.8641 2.3600 2.0931 1.7641 2.1131 1.7748
1.8840 1.7604 1.7664 2.2000 2.0055 1.8229 1.9871 1.9168
1.7340 1.9656 1.8480 2.0523 1.9950 1.8716 1.9206 1.7786


And this is what I'm doing currently:

for (i in data){
x <- i

h<-hist(x, plot = FALSE)
h$density = h$counts/sum(h$counts)

plot(h, col="red", xlim = c(0,max(data)))}


But I get this picture:

enter image description here

But what I really want is something like this (Y axis between 0-1):

enter image description here

Answer

To scale your Y between [0,1] you could use what flodel suggested in this answer

maxs <- apply(a, 2, max)
mins <- apply(a, 2, min)
scale(a, center = mins, scale = maxs - mins)

But modifying that code to use only your y column:

maxs <- max(y)
mins <- min(y)
scale(y, center = mins, scale = maxs - mins)

Hope it helps