How can I plot a histogram of a long-tailed data using R?

后端 未结 3 1812
渐次进展
渐次进展 2020-12-13 16:18

I have data that is mostly centered in a small range (1-10) but there is a significant number of points (say, 10%) which are in (10-1000). I would like to plot a histogram f

3条回答
  •  独厮守ぢ
    2020-12-13 16:49

    Using ggplot2 seems like the most easy option. If you want more control over your axes and your breaks, you can do something like the following :

    EDIT : new code provided

    x <- c(rexp(1000,0.5)+0.5,rexp(100,0.5)*100)
    
    breaks<- c(0,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,10000)
    major <- c(0.1,1,10,100,1000,10000)
    
    
    H <- hist(log10(x),plot=F)
    
    
    plot(H$mids,H$counts,type="n",
          xaxt="n",
          xlab="X",ylab="Counts",
          main="Histogram of X",
          bg="lightgrey"
    )
    abline(v=log10(breaks),col="lightgrey",lty=2)
    abline(v=log10(major),col="lightgrey")
    abline(h=pretty(H$counts),col="lightgrey")
    plot(H,add=T,freq=T,col="blue")
    #Position of ticks
    at <- log10(breaks)
    
    #Creation X axis
    axis(1,at=at,labels=10^at)
    

    This is as close as I can get to the ggplot2. Putting the background grey is not that straightforward, but doable if you define a rectangle with the size of your plot screen and put the background as grey.

    Check all the functions I used, and also ?par. It will allow you to build your own graphs. Hope this helps.

    alt text

提交回复
热议问题