0
$\begingroup$

I am plotting data distribuiton.

I get the expected plot with data which are only integer numbers. But I didn't get appropriate plot for decimal data sets.

The code with integer numbers data as follow:

library(sfsmisc)
dev.new()
M <- 30 # number of bins
r <- (max(data)/min(data))^(1/M) 
brea1 <- seq(1,M,1) 
brea <- c(0, min(data) * (r^(brea1))) # 
hlog <- hist(data,breaks=brea,plot=TRUE)
hlogx <- hlog$mids[hlog$density != 0]
hlogy <- hlog$density[hlog$density != 0]

plot(hlogx,hlogy,log="xy",pch=16,type="p",cex=2,col="steelblue3",xlab="",ylab="",
     las=1,xaxt="n",yaxt="n")
eaxis(1,at=c(1,10,10^2,10^3,10^4),tck=1, cex.axis=1.5)
eaxis(2,at=c(10^-9,10^-8,10^-7,10^-6,10^-5,10^-4,10^-3,10^-2,10^-1,10^0),cex.axis=1.5)
#axis(3, at=hlogx, col="blue", tck = 0.02, las=2) # tck tickmarks inside the graph
$breaks
 [1]   0.000000   4.741919   5.621450   6.664116   7.900175   9.365498  11.102610  13.161920  15.603191
[10]  18.497269  21.928140  25.995369  30.816986  36.532916  43.309037  51.341991  60.864896  72.154108
[19]  85.537242 101.402678 120.210833 142.507522 168.939797 200.274727 237.421656 281.458592 333.663493
[28] 395.551351 468.918161 555.893037 659.000000

$counts
 [1]   13 2877 1796 1198 1392  796  519  309  309  207  152  115   91   58   48   35   24   20   10   12
[21]    6    6    2    1    1    0    1    0    1    1

$density
 [1] 2.741506e-04 3.271063e-01 1.722508e-01 9.692092e-02 9.499610e-02 4.582320e-02 2.520261e-02
 [8] 1.265734e-02 1.067698e-02 6.033453e-03 3.737189e-03 2.385092e-03 1.592042e-03 8.559470e-04
[15] 5.975386e-04 3.675349e-04 2.125923e-04 1.494418e-04 6.303010e-05 6.380211e-05 2.690983e-05
[22] 2.269952e-05 6.382653e-06 2.692013e-06 2.270821e-06 0.000000e+00 1.615826e-06 0.000000e+00
[29] 1.149757e-06 9.698666e-07

$mids
 [1]   2.370960   5.181685   6.142783   7.282145   8.632837  10.234054  12.132265  14.382556  17.050230
[10]  20.212705  23.961754  28.406177  33.674951  39.920976  47.325514  56.103443  66.509502  78.845675
[19]  93.469960 110.806756 131.359177 155.723659 184.607262 218.848192 259.440124 307.561042 364.607422
[28] 432.234756 512.405599 607.446518

The data (integer numbers) is split by 30 bins. The total data is 10000 integer numbers. And hist() density value is normalized by binwidth. According R help: ?hist, the value $f^{(x[i])}$, e.g. hlog$density, satisfies $\sum_i f^{(x[i])}(b[i+1]-b[i]) = 1$. The second value 0.3271063, $f^{(x[2])}(b[3]-b[2])=0.3271063\times(5.62145-4.741919)=0.2877=\frac{counts[2]}{n}=\frac{2877}{10000}$. That means hlog$density is normalized by binwidth, e.g. $\frac{frequency}{binwidth} = \frac{\frac{counts}{n}}{b[i+1]-b[i]}$

Anyway, the result shows well. The y scale is an probability value or frequency value in the plot which less than 1. See $10^{-1}, \cdots, 10^{-5}$

enter image description here



But when I plot another decimal data sets. The result is not what I expected. The y scale is not less than 1.

The code:

dev.new()
bgap <- 0.00001
brea <- seq(-bgap, max(pagerankB)+bgap, bgap)
hlogP <- hist(pagerankB,breaks=brea, plot=TRUE)

hlogPx <- hlogP$mids[hlogP$density!=0]
hlogPy <- hlogP$density[hlogP$density!=0]

plot(hlogPx,hlogPy,log="xy",pch=16,type="p",cex=2,col="steelblue3",
     xlab="",ylab="",
     xlim=c(10^-5,10^-2), 
     ylim=c(10^-5, 10^5),
     las=1,xaxt="n",yaxt="n")
axis(side=1, at=10^(-5:-2), labels = expression(10^-5, 10^-4, 10^-3, 10^-2))
axis(side=2, at=10^(-5:5) ,labels = expression(10^-5, 10^-4,10^-3, 10^-2,10^-1,
                                               10^0,10^1,10^2,10^3,10^4,10^5))
$breaks
 [1] 0.000000e+00 5.445118e-05 6.390038e-05 7.498934e-05 8.800264e-05 1.032742e-04 1.211959e-04
 [8] 1.422277e-04 1.669092e-04 1.958738e-04 2.298648e-04 2.697544e-04 3.165663e-04 3.715017e-04
[15] 4.359704e-04 5.116266e-04 6.004119e-04 7.046045e-04 8.268782e-04 9.703707e-04 1.138764e-03
[22] 1.336380e-03 1.568289e-03 1.840442e-03 2.159824e-03 2.534629e-03 2.974477e-03 3.490653e-03
[29] 4.096405e-03 4.807275e-03 5.641507e-03

$counts
 [1]  255 2871 2278 1272 1000  668  438  350  255  168  112   94   60   45   42   27   17   13   11    7
[21]    7    4    1    1    1    0    1    0    1    1

$density
 [1] 4.683094e+02 3.038353e+04 2.054294e+04 9.774621e+03 6.548121e+03 3.727323e+03 2.082565e+03
 [8] 1.418066e+03 8.803844e+02 4.942484e+02 2.807747e+02 2.008036e+02 1.092192e+02 6.980138e+01
[15] 5.551427e+01 3.041046e+01 1.631594e+01 1.063189e+01 7.665906e+00 4.156930e+00 3.542229e+00
[22] 1.724815e+00 3.674399e-01 3.131051e-01 2.668050e-01 0.000000e+00 1.937322e-01 0.000000e+00
[29] 1.406726e-01 1.198708e-01

$mids
 [1] 2.722559e-05 5.917578e-05 6.944486e-05 8.149599e-05 9.563841e-05 1.122350e-04 1.317118e-04
 [8] 1.545684e-04 1.813915e-04 2.128693e-04 2.498096e-04 2.931604e-04 3.440340e-04 4.037361e-04
[15] 4.737985e-04 5.560192e-04 6.525082e-04 7.657413e-04 8.986244e-04 1.054567e-03 1.237572e-03
[22] 1.452334e-03 1.704366e-03 2.000133e-03 2.347227e-03 2.754553e-03 3.232565e-03 3.793529e-03
[29] 4.451840e-03 5.224391e-03

Obviously, it satisfies $\sum_i f^{(x[i])}(b[i+1]-b[i]) = 1$. The second value is 30383.53. It satisfies $f^{(x2)}(b[3]-b2)=30383.53\times(6.390038e-05 - 5.445118e-05)=0.2871=\frac{counts[2]}{n}=\frac{2871}{10000}$. That means hlog$density is normalized by binwidth, e.g. $\frac{frequency}{binwidth} = \frac{\frac{counts}{n}}{b[i+1]-b[i]}$

The problem is that the values in y scale greater than 1. For example, the second value is 30383.53. It is not a appropriate value for a distribution plot. It seems caused by the normalization process. $\frac{\frac{2871}{10000}}{(6.390038e-05 - 5.445118e-05)} = \frac{0.2871}{(6.390038e-05 - 5.445118e-05)} = 30383.53$

enter image description here

The questions:

It is useful to normalized the data frequency by binwdith? When should normalized the data frequency by binwdith?

Since it works appropriately for integer number data sets, does that means it should not normalized it by binwidth for decimal data sets?

how to normalize binning decimal data when plot distribution in r?

does that mean I should not using histogram to plot data distribution?

Thank your for your time. If I am not described my question clearly, please comment below.

$\endgroup$

migrated from stackoverflow.com Mar 19 at 18:43

This question came from our site for professional and enthusiast programmers.

  • $\begingroup$ So, that is not a problem which can be solved by R code? $\endgroup$ – Nick Dong Mar 19 at 15:28
  • $\begingroup$ Honestly I can't tell what your question is here. I see "Is it useful to normalized the data frequency by binwdith?" and that's not an R question. I see "When should normalized the data frequency by binwdith?" and that's not an R question. What exactly is the problem you are trying to solve? Is it specific to R code or do you not know how to do a particular task with any programming language? $\endgroup$ – MrFlick Mar 19 at 15:31
  • $\begingroup$ Yes, you are right. But I also trying to find an alternative way in R which could normlize data frequency by binwidth in a distribution plot. I am not sure if it is possible. $\endgroup$ – Nick Dong Mar 19 at 15:38
  • $\begingroup$ If you are not even sure if that's possible, then that sounds like a more theoretical question. And as already mentioned, such questions belong on Cross Validated, not here. If this is a programming question your description of it just isn't very clear. What exactly is the input you have and the output you desire? $\endgroup$ – MrFlick Mar 19 at 15:40
  • $\begingroup$ Ok. I close the question first. And then ask my question on Cross Validated. Thank you. $\endgroup$ – Nick Dong Mar 19 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.