Force R to plot histogram as probability (relative frequency)

前端 未结 5 2041
面向向阳花
面向向阳花 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:28

    Are you sure? This is working for me:

    > vec <- rnorm(6000000)
    > 
    > h <- hist(vec, breaks = 800, freq = FALSE)
    > sum(h$density)
    [1] 100
    > unique(zapsmall(diff(h$breaks)))
    [1] 0.01
    

    Multiply the last two results and you get a probability density sum of 1. Remember that the bin width is important here.

    This is with

    > sessionInfo()
    R version 3.0.1 RC (2013-05-11 r62732)
    Platform: x86_64-unknown-linux-gnu (64-bit)
    
    locale:
     [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
     [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
     [7] LC_PAPER=C                 LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    loaded via a namespace (and not attached):
    [1] tools_3.0.1
    

提交回复
热议问题