繁体   English   中英

如何通过R中的x值缩放直方图上的y轴?

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

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

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

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

如果您真的想将每个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

您最好使用条形图,以便通过垃圾箱的面积表示总质量(即,高度表示计数,宽度表示质量):

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)

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

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

# 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)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM