orizon
orizon

Reputation: 3239

Lattice barchart has incorrect tick labels

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?

Upvotes: 3

Views: 783

Answers (1)

rcs
rcs

Reputation: 68849

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

Upvotes: 3

Related Questions