Search code examples
rscalehistogram

How do I scale the y-axis on a histogram by the x values in R?


I have some data which represents a sizes of particles. I want to plot the frequency of each binned-size of particles as a histogram, but scale the frequency but the size of the particle (so it represents total mass at that size.)

I can plot a histogram fine, but I am unsure how to scale the Y-axis by the X-value of each bin.

e.g. if I have 10 particles in the 40-60 bin, I want the Y-axis value to be 10*50=500.


Solution

  • You would better use barplot in order to represent the total mass by the area of the bins (i.e. height gives the count, width gives the mass):

    sizes <- 3:10   #your sizes    
    part.type <- sample(sizes, 1000, replace = T)  #your particle sizes  
    
    count <- table(part.type)  
    barplot(count, width = size)  
    

    If your particle sizes are all different, you should first cut the range into appropriate number of intervals in order to create part.type factor:

    part <- rchisq(1000, 10)  
    part.type <- cut(part, 4)  
    
    count <- table(part.type)  
    barplot(count, width = size)  
    

    If the quantity of interest is only total mass. Then, the appropriate plot is the dotchart. It is also much clearer comparing to the bar plot for a large number of sizes:

    part <- rchisq(1000, 10)
    part.type <- cut(part, 20)
    
    count <- table(part.type)
    dotchart(count)
    

    Representing the total mass with bins would be misleading because the area of the bins is meaningless.