【问题标题】:How do I scale the y-axis on a histogram by the x values in R?如何通过 R 中的 x 值缩放直方图上的 y 轴?
【发布时间】:2010-10-13 09:14:17
【问题描述】:

我有一些代表粒子大小的数据。我想将每个分箱大小的粒子的频率绘制为直方图,但要缩放频率但要缩放粒子的大小(因此它代表该大小的总质量。)

我可以很好地绘制直方图,但我不确定如何通过每个 bin 的 X 值来缩放 Y 轴。

例如如果我在 40-60 bin 中有 10 个粒子,我希望 Y 轴值为 10*50=500。

【问题讨论】:

  • 我会创建一个自定义变量来保存“比例”值并使用它来生成条形图。
  • 总质量是一个体积概念(即 count*size)。并且您尝试用 "histogram" 的高度在一维中表示它。我在下面的回答,将计数保留为条的高度,但按大小缩放宽度。所以你得到了由垃圾箱面积表示的总质量。您的对象不再是直方图,人们会发现很难解释。如果您仍然想要原始对象,只需按照 Roman 的建议计算总质量并使用条形图绘制。
  • 给定大小的总质量是需要绘制的,因此它与色谱机的输出相当,其中吸光度与通过特定质量颗粒的物质的总质量成正比.这当然不太直观,但更相关!
  • 这是一个有效的绘图数量。用条来表示它有点误导,因为每个条的面积没有意义。点图是您案例的完美候选者。我已经更新了我的答案。

标签: r scale histogram


【解决方案1】:

您最好使用条形图以按箱的面积表示总质量(即高度给出计数,宽度给出质量):

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

count <- table(part.type)  
barplot(count, width = size)  

如果你的粒子大小都不同,你应该首先将范围切割成适当数量的间隔,以创建 part.type 因子:

part <- rchisq(1000, 10)  
part.type <- cut(part, 4)  

count <- table(part.type)  
barplot(count, width = size)  

如果感兴趣的数量只是总质量。然后,适当的图是点图。与大量尺寸的条形图相比,它也更清晰:

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

count <- table(part.type)
dotchart(count)

用 bin 表示总质量会产生误导,因为 bin 的面积没有意义。

【讨论】:

  • MASS 中有一个函数 truehist 可以使 bin 的区域有意义。
【解决方案2】:

如果你真的想使用每个 bin 的中点作为比例因子:

d<-rgamma(100,5,1.5) # sample
z<-hist(d,plot=FALSE) # make histogram, i.e., divide into bins and count up
co<-z$counts # original counts of each bin
z$counts<-z$counts*z$mids # scaled by mids of the bin

plot(z, xlim=c(0,10),ylim=c(0,max(z$counts))) # plot scaled histogram
par(new=T)
plot(z$mids,co,col=2,  xlim=c(0,10),ylim=c(0,max(z$counts))) # overplot original counts

相反,如果你想使用每个采样点的实际值作为比例因子:

d<-rgamma(100,5,1.5)
z<-hist(d,plot=FALSE)
co<-z$counts # original counts of each bin
z$counts<-aggregate(d,list(cut(d,z$breaks)),sum)$x # sum up the value of data in each bin

plot(z, xlim=c(0,10),ylim=c(0,max(z$counts))) # plot scaled histogram
par(new=T)
plot(z$mids,co,col=2,  xlim=c(0,10),ylim=c(0,max(z$counts))) # overplot original counts

【讨论】:

    【解决方案3】:

    只需隐藏坐标轴并根据需要重新绘制它们。

    # Generate some dummy data
    datapoints <- runif(10000, 0, 100)
    
    par (mfrow = c(2,2))
    
    # We will plot 4 histograms, with different bin size
    binsize <- c(1, 5, 10, 20)
    
    for (bs in binsize)
        {
        # Plot the histogram. Hide the axes by setting axes=FALSE
        h <- hist(datapoints, seq(0, 100, bs), col="black", axes=FALSE, 
            xlab="", ylab="", main=paste("Bin size: ", bs))
        # Plot the x axis without modifying it
        axis(1)
        # This will NOT plot the axis (lty=0, labels=FALSE), but it will return the tick values
        yax <- axis(2, lty=0, labels=FALSE)
        # Plot the axis by appropriately scaling the tick values
        axis(2, at=yax, labels=yax/bs)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2017-08-31
      • 2022-06-14
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2014-05-09
      相关资源
      最近更新 更多