【问题标题】:Create multiple histograms for each level of a variable (factor) with scales of each own为变量(因子)的每个级别创建多个直方图,每个级别都有自己的比例
【发布时间】:2013-06-12 22:57:38
【问题描述】:

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

有没有办法在一张图 (3X9) 上显示 27 个直方图,不叠加?

我用 ggplot2 试过这个:

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

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

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

【问题讨论】:

  • facet_wrap(~zip,ncol=9, scales="free")?
  • @baptiste 是的,没错。

标签: r ggplot2 histogram


【解决方案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") ) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2012-09-21
    相关资源
    最近更新 更多