为每个变量(因子)级别创建多个直方图,每个级别都有自己的比例

时间:2021-07-03 14:58:44

I want to create 27 histograms of the variable "billed" (numeric, x-axis) for each level of a factor variable "zip" (y-axis is "count"). "zip" has 27 levels.

我想为因子变量“zip”(y轴是“count”)的每个级别创建27个变量“billed”(数字,x轴)的直方图。 “zip”有27个级别。

Is there a way to display 27 histograms on one graph (3X9), no over-laid?

有没有办法在一个图表(3X9)上显示27个直方图,没有覆盖?

I tried this using ggplot2:

我尝试使用ggplot2:

p<-ggplot(dat,aes(x=billed))+geom_histogram(aes(fill=zip),binwidth=1.5)
+facet_wrap(~zip,ncol=9)

The new problem is all these histograms have the same scale. But my data's y-axis/x-axis vary a lot among different zips. Is there a way to create these histograms based on their own scales?

新问题是所有这些直方图具有相同的比例。但我的数据的y轴/ x轴在不同的拉链之间变化很大。有没有办法根据自己的尺度创建这些直方图?

I don't mind using regular r function if this could also be realized by hist(), since the aesthetic features in ggplot2 are not useful for my case.

我不介意使用常规的r函数,如果这也可以通过hist()来实现,因为ggplot2中的美学特征对我的情况没有用。

1 个解决方案

#1


1  

require(lattice)
histogram( ~ billed | zip , data=dat, 
           layout=c(3,9) , scales= list(y=list(relation="free"),
                                        x=list(relation="free") ) )

 #worked example from ?histogram page:
 histogram( ~ height | voice.part, data = singer, 
            layout = c(2,4), scales=list(y=list(relation="free"),
                                         x=list(relation="free") ) )

#1


1  

require(lattice)
histogram( ~ billed | zip , data=dat, 
           layout=c(3,9) , scales= list(y=list(relation="free"),
                                        x=list(relation="free") ) )

 #worked example from ?histogram page:
 histogram( ~ height | voice.part, data = singer, 
            layout = c(2,4), scales=list(y=list(relation="free"),
                                         x=list(relation="free") ) )