2014-10-06 68 views
0

我想在格子bwplot的每個盒子和鬚子上標註一些統計信息。 下面是一個通用示例。R格子bwplots的標籤

#---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.必須訪問的東西,例如,長度,平均的方式,中位數等每個盒須在每面板?

回答

0

我認爲你必須自己計算這些值,這對於例如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 
    } 
    ) 
+0

非常感謝您迅速解決此問題。然而,我發現在一個真實的數據例子中,我並沒有很好地工作,每個面板的每個盒子和每個面板都有不同數量的數據,而且當沒有數據時也有數據。然而,這個解決方案當然指出了我正確的方向,因爲我瞭解到數字標籤必須與每個面板中的盒子和鬍鬚圖的數量相匹配。通過使用「if(packet.number()== 1){ltext(」等等,如果我爲每個面板設置一組標籤,我可以將標籤設置到正確的位置和正確的順序。 – Markm0705 2014-10-07 07:59:57