【问题标题】:R lattice Labels for bwplots用于 bwplots 的 R 格子标签
【发布时间】:2014-10-06 12:41:07
【问题描述】:

我想在格子 bwplot 中标记每个框和晶须上的一些统计数据。 下面是一个通用示例。

#---Some dummy data
Rock<-c("Rock1","Rock2","Rock3")
Zone<-as.data.frame(c("Zone10","Zone11","Zone12"))
Domain<-as.data.frame(c("Domain1","Domain2"))
Dt <- as.data.frame(rnorm(100))
Dt<-merge(Dt,Zone)
Dt<-merge(Dt,Rock)
Dt<-merge(Dt,Domain)
names(Dt)<-c("Data","Zone","Rock","Domain")

#--- Use aggregate to get the number of values for each combination of three factors (100 each)
aggregate(Data~Rock*Zone*Domain,Dt,FUN=length)

require(lattice)
#--- create a lattice plot and attempt to label the number of value associated with each BnW
bwplot(Rock~Data|Zone*Domain,
    data=Dt,
    xlim=c(-5,5),
       panel=function(...){
        panel.bwplot(...)
        panel.text(-4,c(1,2,3),length(x))
    }

)

这不起作用 - 不知道为什么我得到的标签是 45 而不是 100。必须有一种方法可以访问每个面板中每个框和胡须的长度、平均值、中值等内容吗?

【问题讨论】:

    标签: r lattice labels bwplot


    【解决方案1】:

    我认为您必须自己计算值,这很容易使用例如 plyr 包(尽管您也可以继续使用聚合)。

    library(plyr)
    agg=ddply(Dt,c("Zone","Rock","Domain"),summarise,length=length(Data), max=max(Data),median=median(Data))  # max or median or ... to have the xvalues were you want to plot the values of length
    
    require(lattice)
    bwplot(Rock~Data|Zone*Domain,
       data=Dt,
       xlim=c(-5,5),
       panel=function(...){
         panel.bwplot(...)
         panel.text(agg$max+1,c(1,2,3),agg$length)  # plus one to not mask the last dot
       }
       )
    

    【讨论】:

    • 非常感谢您对这个问题的及时解决。但是,我发现它在真实数据示例中效果不佳,其中每个面板中每个框和胡须的数据数量不同,并且在没有数据的关卡中也是如此。然而,这个解决方案确实为我指明了正确的方向,因为我了解到数字标签必须与每个面板中的箱线图和晶须图的数量相匹配。通过使用 "if(packet.number()==1) {ltext(" 等等,如果我为每个面板设置一组标签,我可以将标签设置为正确的位置和正确的顺序。
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多