Christine SINOQUET
2011-Jun-09 16:22 UTC
[R] histogram - density on y axis and restriction to interval [0, 1]
Hello, To indicate probability densities instead of counts on a histogram, I specify freq = FALSE. However, I expect that summing all top y coordinates over all the intervals of the histogram will provide 1. 1) v <- c(0.2885, 0.2988, 0.3139, 0.2615, 0.3179, 0.3163, 0.2583, 0.3052, 0.2527, 0.3147, 0.3235, 0.2408, 0.2480, 0.3108, 0.3577, 0.2829, 0.2694, 0.3275, 0.3314, 0.2639, 0.3076, 0.3346, 0.2933, 0.3585, 0.2678, 0.3338) hist(v, freq = FALSE) With the above example, roughly, I obtain 3 * 8 + 10 + 14 + 4 = 52 Besides, I do not understand the units on the y axis. I would have expected decimals. 2) In contrast, I am satisfied with the units on the y axis with the example below : v <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9) hist(v, freq = FALSE) 3) Again, with the example below, I wonder why the unit axis does not show the interval [0,1] (or maybe [0%,100%]). Proportions are correct but the indication 1.4 is not usual. v <- c(1,2,2,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4) > hist(v, freq = FALSE) I thank you in advance for any explanation about the way to force the y axis to restrain to interval [0,1] on an histogram. Best regards, Christine Sinoquet
Sarah Goslee
2011-Jun-09 16:31 UTC
[R] histogram - density on y axis and restriction to interval [0, 1]
Did you read the help for hist? freq: logical; if ?TRUE?, the histogram graphic is a representation of frequencies, the ?counts? component of the result; if ?FALSE?, probability densities, component ?density?, are plotted (so that the histogram has a total area of one). Defaults to ?TRUE? _if and only if_ ?breaks? are equidistant (and ?probability? is not specified). freq=FALSE isn't doing what you think it is. But what I think you want is easy enough to accomplish. v.hist <- hist(v, plot=FALSE) v.hist$counts <- v.hist$counts/sum(v.hist$counts) plot(v.hist) Sarah On Thu, Jun 9, 2011 at 12:22 PM, Christine SINOQUET <christine.sinoquet at univ-nantes.fr> wrote:> Hello, > > To indicate probability densities instead of counts on a histogram, I > specify freq = FALSE. > > However, I expect that summing all top y coordinates over all the intervals > of the histogram will provide 1. > > 1) > v <- c(0.2885, 0.2988, 0.3139, 0.2615, 0.3179, 0.3163, 0.2583, 0.3052, > 0.2527, 0.3147, 0.3235, 0.2408, 0.2480, 0.3108, 0.3577, 0.2829, 0.2694, > 0.3275, 0.3314, 0.2639, 0.3076, 0.3346, 0.2933, 0.3585, 0.2678, 0.3338) > > hist(v, freq = FALSE) > > With the above example, roughly, I obtain 3 * 8 + 10 + 14 + 4 = 52 > > Besides, I do not understand the units on the y axis. I would have expected > decimals. > > 2) In contrast, I am satisfied with the units on the y axis with the example > below : > ?v <- > c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9) > hist(v, freq = FALSE) > > > 3) Again, with the example below, I wonder why the unit axis does not show > the interval [0,1] (or maybe [0%,100%]). Proportions are correct but the > indication 1.4 is not usual. > > v <- c(1,2,2,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4) >> hist(v, freq = FALSE) > > > I thank you in advance for any explanation about the way to force the y axis > to restrain to interval [0,1] on an histogram. > > Best regards, > > Christine Sinoquet > > __________-- Sarah Goslee http://www.functionaldiversity.org