Skip to content

Lattice Barchart

2 messages · Pete Brecknock, Richard M. Heiberger

#
Pete Brecknock wrote
I put together the following solution but would be interested in any other
approaches people may have to share.

library(lattice) 
library(latticeExtra)

d0 <-
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"),
NULL)) 
mycols <- c("red","brown","orange") 

d1 <- data.frame("Dt"=row.names(d0), "Sum"=rowSums(d0))

barchart(d0, 
         horizontal=FALSE, 
         stack=TRUE, 
         auto.key=list(text=c("A","B","C"), 
           columns =3, 
           title="", 
           cex.title =0.9, 
           border=FALSE), 
         xlab="Month", 
         ylab="Difference", 
         main="Stacked Barchart",
         par.settings = simpleTheme(col = mycols)) +

as.layer(xyplot(Sum~Dt, data=d1, type="o", pch=19, cex=1.8, col="black",
lwd=3), y.same=TRUE)

Thanks 

Pete



--
View this message in context: http://r.789695.n4.nabble.com/Lattice-Barchart-tp4685387p4685400.html
Sent from the R help mailing list archive at Nabble.com.
#
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: