[R] Lattice Barchart

Pete Brecknock Peter.Brecknock at bp.com
Sun Feb 16 19:05:23 CET 2014


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.



More information about the R-help mailing list