Force R to plot histogram as probability (relative frequency)

前端 未结 5 2039
面向向阳花
面向向阳花 2021-01-31 05:40

I am having trouble plotting a histogram as a pdf (probability)

I want the sum of all the pieces to equal an area of one so it\'s easier to compare across datasets. For

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 06:17

    I observed that, in histogram density = relative frequency / corresponding bin width

    Example 1:

    nums = c(10, 41, 10, 28, 22, 8, 31, 3, 9, 9)

    h2 = hist(nums, plot=F)

    rf2 = h2$counts / sum(h2$counts)

    d2 = rf2 / diff(h2$breaks)

    h2$density

    [1] 0.06 0.00 0.02 0.01 0.01

    d2

    [1] 0.06 0.00 0.02 0.01 0.01

    Example 2:

    nums = c(10, 41, 10, 28, 22, 8, 31, 3, 9, 9)

    h3 = hist(nums, plot=F, breaks=c(1,30,40,50))

    rf3 = h3$counts / sum(h3$counts)

    d3 = rf3 / diff(h3$breaks)

    h3$density

    [1] 0.02758621 0.01000000 0.01000000

    d3

    [1] 0.02758621 0.01000000 0.01000000

提交回复
热议问题