0

我有数据框

df <- data.frame(
  site=rep(c("s1","s2","s3"),3),
  grp=c("a","a","a","b","b","b","c","c","c"),
  total=c(0,1,2,0,4,6,8,0,2)
)

df

 site grp total
1   s1     a     0
2   s2     a     1
3   s3     a     2
4   s1     b     0
5   s2     b     4
6   s3     b     6
7   s1     c     8
8   s2     c     0
9   s3     c     2

#that I use to create the following lattice plot

library(lattice)

median<-with(df,reorder(grp,total,mean))
df$median<-median
df1<-subset(df,total!=0)

barchart(df$site~total|median,data=df, xlim=c(0,10), col="grey", border="NA", 
         par.settings=list(axis.text=list(cex=0.85), fontsize=list(text=10)), 
         par.strip.text=list(cex=0.9), par.strip.col="white", layout = c(3,2), 
         aspect = (0.3),scales=list(y=list(relation="free"))
)

我想均匀地间隔条形,并通过减少 df$grp (iea,b,c) 的每个级别的 df$total 来排列它们。有任何想法吗?谢谢

4

2 回答 2

1
df$median<-median
df1<-subset(df,total!=0)

然后使用df1,但也许我错过了一些东西

于 2012-07-17T12:10:10.203 回答
0

在另一个答案中提供了使用df1它,以及一些自定义预面板和面板:

barchart(site ~ total | median, data = df1, 
         prepanel = function(x, y, ...) {
           xx <- reorder(droplevels(y), x)
           list(ylim = levels(xx),
                yat = sort(unique(as.numeric(xx))))
         },
         panel = function(x, y, ...) {
           xx <- reorder(droplevels(y), x)
           panel.barchart(x, xx, ...)
         }, scales = list(y = list(relation = "free")))

伊姆古尔

PS:我还清理了通话中的一些绒毛。

于 2017-02-27T09:47:02.380 回答