0

I have the dataframe

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"))
)

I would like to space the bars evenly and also arrange them by decreasing df$total for each level of df$grp (i.e.a,b,c). Any ideas? Thanks

Andrie
  • 176,377
  • 47
  • 447
  • 496
Elizabeth
  • 6,391
  • 17
  • 62
  • 90

2 Answers2

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

then use df1 but maybe im missing something

shhhhimhuntingrabbits
  • 7,397
  • 2
  • 23
  • 23
  • Thanks. That removed the labels for the 0 values but the spacing is still uneven (i.e.there are still spaces for bars even though there are no labels. Is there anyway to make the bars on each graph evenly spaced with y axis that are independent the y axis on other graphs? Thanks again. – Elizabeth Jul 17 '12 at 12:35
  • I added your edits to the code and tried to clarify the question a bit better. Any ideas on how to create even bar spacing and arrange the bars in decreasing size for each grp level (i.e. a,b,c) ? Thanks a million – Elizabeth Jul 17 '12 at 13:24
0

Using df1 that was provided in another answer, plus some custom prepanels and panels:

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")))

Imgur

P.S.: I also cleaned out some of the fluff from the call.

Johan Larsson
  • 3,496
  • 18
  • 34