[R] Lattice Barchart

Richard M. Heiberger rmh at temple.edu
Sun Feb 16 23:32:16 CET 2014


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:
> Pete Brecknock wrote
>> Hi
>>
>> The code below plots a stacked barchart.
>>
>> I would like to overlay on this chart a circular plotting character at the
>> sum of the bars for each month. The plotted characters should be joined
>> with a line.
>>
>> So, for "1/1/2014", I would like to see a point at 200 (-1000+1000+200).
>> For "2/1/2014" a point at 600 (-2000+2000+600) and so on.
>>
>> # Barchart Plot
>> library(lattice)
>>
>> 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")
>> 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))
>>
>> Any pointers would be gratefully received.
>>
>> Kind regards
>>
>> Pete
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list