Search code examples
rbar-chartlattice

set the x-axis to meet y-axis at 0 with lattice


I am struggling with some plots in R. At the moment I am running R version: R version 3.2.0 (2015-04-16) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 (build 7601) Service Pack 1

I really hope someone can give me some advise on how to set my x-axis to meet the y-axis at 0. I am using lattice to create a barchart with error-bars created using a standard deviation values but it seems that the x-axis won't start at 0 while intersecting the y-axis. As you can see from here:

R_barchar_with_error_bars

I am using this dataset "order":

 Order    Area   Count        Stdev
order1  Area 1  224122  37263.35097
order2  Area 1    2091  253.0564825
order3  Area 1   45867  4450.600737
order4  Area 1   32816  4563.089816
order1  Area 2   71548  2046.367025
order2  Area 2     309  24.74873734
order3  Area 2   22564  315.3696244
order4  Area 2   10686  987.1210665

And a friend helped me to have this code working:

#it is better to prepare the prepanel before with all the setting 
prepanel=function(y, Stdev, subscripts=subscripts, ...){
  uy <- as.numeric(y+Stdev[subscripts])#upper y so max value for error bar
  ly <- as.numeric(y-Stdev[subscripts])#lower y so min value for error bar
  list(ylim=range(y,uy,ly, finite=TRUE))
}
panel.err=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
  d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
  g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
  panel.arrows(as.numeric(x)+g,y-Stdev[subscripts], as.numeric(x)+g, y+Stdev[subscripts],
               code=3,angle=90, length=0.025)
}

barchart(Count~Order,#tells it what x and y are for the plot
         groups=Area,#tells it the subdivision in x
         data=order,#tells it where to get the info from
         Stdev=order$Stdev,
         auto.key=list(points=FALSE,rectangles=TRUE,columns=2, title="Area",cex.title=1),#adds the key how to spread the legend and the title of it
         main="Order per Area", #this create the title of the graph 
         #scales=list(x=list(rot=0))), #rotate the x-axis labels 
         par.settings=list(superpose.polygon=list(col=grey.colors(2))),
         prepanel=prepanel,
         panel=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
           panel.barchart(x, y, subscripts=subscripts,
                          groups=groups, box.ratio=box.ratio, ...)
           panel.err(x, y, subscripts=subscripts,
                     groups=groups, box.ratio=box.ratio, Stdev=order$Stdev)
         }
)

I have tried to set xaxs='i' and yaxs='i' or even set xlim=(0,) but it won't do the trick, so I was wondering if anyone have any idea on how to sort it.


Solution

  • Yes, yaxs doesn't work in lattice. See here for how it is handled.

    However, I got this to work by skipping the prepanel and calculating the ylim directly in the barchart call.

    library(lattice)
    order=data.frame(Order=rep(paste0("order",1:4),times=2),
                     Area=rep(paste0("Area ",1:2),each=4),
                     Count=c(224122,2091,45867,32816,71548,309,22564,10686),
                     Stdev=c(37263,253,4450,4563,2046,25,315,987))
    
    panel.err=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
      d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
      g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
      panel.arrows(as.numeric(x)+g,y-Stdev[subscripts], as.numeric(x)+g, y+Stdev[subscripts],
                   code=3,angle=90, length=0.025)
    }
    
    barchart(Count~Order,#tells it what x and y are for the plot
             groups=Area,#tells it the subdivision in x
             data=order,#tells it where to get the info from
             Stdev=order$Stdev,
             auto.key=list(points=FALSE,rectangles=TRUE,columns=2, title="Area",cex.title=1),#adds the key how to spread the legend and the title of it
             main="Order per Area", #this create the title of the graph 
             #scales=list(x=list(rot=0))), #rotate the x-axis labels 
             par.settings=list(superpose.polygon=list(col=grey.colors(2))),
             ylim=c(0,max(order$Count+order$Stdev)*1.04),
             panel=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
               panel.barchart(x, y, subscripts=subscripts,
                              groups=groups, box.ratio=box.ratio, ...)
               panel.err(x, y, subscripts=subscripts,
                         groups=groups, box.ratio=box.ratio, Stdev=order$Stdev)
             }
    )
    

    I added 4% to the top limit so the error bar doesn't fall on the bounding box. I used zero for the lower limit rather than some other minimum value in the data since that is more appropriate for bar charts.