如何通过R中的x值缩放直方图上的y轴?
问题描述:
我有一些数据代表粒子的大小。我想绘制每个分级大小的粒子作为直方图的频率,但缩放频率但是粒子的大小(因此它代表了该大小的总质量)。如何通过R中的x值缩放直方图上的y轴?
我可以绘制直方图,但我不确定如何按每个箱的X值缩放Y轴。
例如如果我在40-60格中有10个粒子,我希望Y轴值为10 * 50 = 500。
答
你会更好,以便通过箱的面积代表总质量使用barplot(即高度给出了计数,宽度给人的质量):
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)
代表与箱总质量会产生误导,因为该箱的区域是没有意义的。
+1
MASS中有一个函数truehist,它使得箱的区域有意义。 – 2013-10-29 20:54:53
答
,如果你真的想用中点每个格子的比例因子:
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
答
只需隐藏轴并根据需要重新绘制它们即可。
# 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)
}
我会创建一个自定义变量来保存“缩放”值并使用它来生成条形图。 – 2010-10-13 10:04:46
总质量是一个体积概念(即计数*大小)。你试图用“直方图”的高度来表示它。我在下面的答案中,将计数保持为条的高度,但将宽度按比例缩放。因此,您可以获得由垃圾箱面积表示的总质量。你的对象不再是直方图了,人们会发现它很难解释。如果你仍然想要你的原始物体,只需按照罗马的建议计算你的总质量和绘图。 – VitoshKa 2010-10-13 10:55:31
在给定尺寸下的总质量是需要绘制的,因此它与色谱仪的输出相当,其中吸光度与在特定质量的粒子处通过的总质量成比例。这当然不太直观,但它更相关! – Nick 2010-10-13 16:12:51