0

Dear stackoverflow experts,

I'm building barcharts with lattice. I have two separate table, one with means and other with standard errors. How can I include the standard errors in the bars?

table.df<-means 
table.std<-table_error
library(lattice)
library(gridExtra)
graph1<-barchart(value1~Var1|Var2+Var3,data=table.df, ylab=NULL)
graph2<-barchart(value2~Var1|Var2+Var3,data=table.df, ylab=NULL)
grid.arrange(graph1,graph2, nrow=2, ncol=2)

Any suggestion? Is this possible?

barchart example

rcs
  • 67,191
  • 22
  • 172
  • 153
Francisco
  • 381
  • 3
  • 4
  • 13
  • 1
    Tried seeing if the large whitespace was in the text, but it's in the image, so not amenable to editing. I think I have tried (for many hours) finding a way to have "super-headers" or what might be called "super-strips" in lattice lingo, but I have come up empty. I hope someone else has a solution. (The right term is "strip", rather than "header" if you are doing any searching of the archives.) – IRTFM Dec 13 '12 at 18:37
  • 1
    This seems to also be a question about creating "dynamite plots". That design is considered "poor form" by many. Here's Frank Harrell's take on it: http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/DynamitePlots . So the advice would be to use a better plot design, such as boxplots or violinplots. – IRTFM Dec 13 '12 at 19:04
  • 1
    There are two completely distinct questions here and it'd be better to split them into two separate posts. – Josh O'Brien Dec 13 '12 at 19:20
  • I followed your suggestion Josh.Thank´s DWin, I´m probably going to use bwplot(), but I´m curious to see how this is done in lattice. – Francisco Dec 13 '12 at 22:22
  • Are you confined to `lattice` or do you accept `ggplot2` solutions as well? – Roman Luštrik Dec 14 '12 at 11:13
  • I Roman, I know how to do it in ggplot2. Thank´s anyway – Francisco Dec 14 '12 at 15:06

1 Answers1

0

While I agree with the comments that this is probably a bad idea, here is a solution anyway:

x <- rnorm(100, 10, 1)
ind <- sample(c("one", "two"), 100, TRUE)

dd <- data.frame(
  m = tapply(x, ind, mean),
  ind = unique(ind),
  se = tapply(x, ind, function(x) {sd(x) / sqrt(length(x))})
)

lattice::barchart(ind ~ m, data = dd, xlim = c(8, 12),
                  se = dd$se,
                  panel = function(x, y, ...) {
                    panel.barchart(x, y, ...)
                    panel.segments(y0 = y, x0 = x - dd$se,
                                   y1 = y, x1 = x + dd$se, ...)
                  })

Imgur

Johan Larsson
  • 3,496
  • 18
  • 34