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

发布于 2024-09-27 09:50:45 字数 176 浏览 1 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-10-04 09:50:45

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

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)

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

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.

笑忘罢 2024-10-04 09:50:45

如果您确实想使用每个 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

if you really want to use the mid point of each bin as a scaling factor:

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

instead, if you want to use the actual value of each sample point as a scaling factor:

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
岁月无声 2024-10-04 09:50:45

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

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

Just hide the axes and replot them as needed.

# 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)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文