Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lattice barchart has incorrect tick labels

Tags:

r

lattice

I am trying to create a bar chart in lattice but the x-axis tick marks are not showing correctly:

dd <- data.frame(Year = 1990:1999, Count = 0:9)
barchart(Count ~ Year, data = dd, horizontal = FALSE)

enter image description here

The tick labels for the x-axis should be 1990, 1991, ... not 1, 2.

I do not want to convert year to a factor before plotting because I need to combine the barchart with an xyplot using c.trellis( ..., x.same = TRUE) in latticeExtra, which does not seem to work with factor axes.

How can I fix this?

like image 553
orizon Avatar asked Jan 01 '26 02:01

orizon


1 Answers

You could use a custom axis function:

barchart(Count ~ Year, data=dd, horizontal=FALSE,
         axis=function(side, ...) { 
           if (side=="bottom") 
             panel.axis(at=seq_along(dd$Year), label=dd$Year, outside=TRUE, rot=0, tck=0) 
           else axis.default(side, ...)
         })

barchart with custom axis function

like image 188
rcs Avatar answered Jan 02 '26 17:01

rcs