Skip to content
Prev 336700 / 398513 Next

Lattice Barchart

Pete,

Thank you for this example.  I recommend using the likert function in
the HH package.

d2 <-
structure(c(1000, 2000, 2500, 5000, 1000, 2000, 3000, 2000, 200,
600, 1000, 900), .Dim = c(4L, 3L), .Dimnames = list(c("1/1/2014",
"2/1/2014", "3/1/2014", "4/1/2014"), c("A", "B", "C")))

d2


likert( ~ A+B+C, data=d2, ReferenceZero=1.5, horizontal=FALSE,
       xTickLabelsPositive=FALSE,
       xlab="Month", ylab="Difference",
       panel=function(x, y, ...) {
         panel.barchart(x, y, ...)
         panel.xyplot(x=factor(levels(x)),
                      y=tapply(y, x, sum),
                      type="b")
       },
       main="minimal version using likert")


## I put the legend on the right to keep it in the same vertical
## relation as the bars in the plot.

likert( ~ A+B+C, data=d2, ReferenceZero=1.5, horizontal=FALSE,
       xTickLabelsPositive=FALSE,
       xlab="Month", ylab="Difference",
       panel=function(x, y, col, col.sum="black", ...) {
         panel.barchart(x, y, ..., col=col)
         panel.xyplot(x=factor(levels(x)),
                      y=tapply(y, x, sum),
                      type="b", ..., col=col.sum)
       },
       main="likert with additional arguments",
       col=c("red","brown","orange"), ## likert colors
       ## col.sum="black",            ## optional. col.sum defaults to "black"
       pch=19, cex=2, lwd=2           ## sum line arguments.
)


## The likert function is defined for positive arguments representing
## counts in various categories.  If your 'A' 'B' 'C' correspond to
## 'Disagree' 'Agree' 'Strongly Agree', then this example can be used
## as is.  If your 'A' 'B' 'C' have some other type of interpretation,
## then additional work will be needed.


Rich
On Sun, Feb 16, 2014 at 1:05 PM, Pete Brecknock <Peter.Brecknock at bp.com> wrote: