[R] Lattice barchart with error bars

175 views
Skip to first unread message

Shaun Jackman

unread,
Jul 4, 2013, 5:53:24 PM7/4/13
to R help
Hi,

I'd like to draw a lattice barchart of means with error bars to show
the standard deviation. I have the barchart, how do I add the error
bars?

require(datasets)
require(lattice)
x <- aggregate(weight ~ Diet, ChickWeight, function(x) c(mean=mean(x),
sd=sd(x)))
barchart(weight[,'mean'] ~ Diet, x)

Thanks,
Shaun

______________________________________________
R-h...@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.

Bert Gunter

unread,
Jul 4, 2013, 6:30:14 PM7/4/13
to Shaun Jackman, R help
Shaun:

I understand that this type of plot is standard in many disciplines,
but it really is awful (google on 'Dynamite plots' for some more
erudite perspectives). Have you considered bwplot() for your
unaggregated data instead?

(No need to reply. It's July 4, and I'm just waving a little flag for
better graphs).

Cheers,
Bert
--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

Shaun Jackman

unread,
Jul 5, 2013, 2:15:46 PM7/5/13
to Dennis Murphy, Bert Gunter, R help
Hi Bert, Dennis,

I'll agree that using a barchart was a poor choice. I was in fact using a
notched bwplot to show the median and confidence interval of the median. In
this case it's the median and confidence interval that I want to highlight,
and I find that the visual noise of the box and whiskers is detracting from
the focus, and those wee notches are not much to focus on. So, I'd like to
draw a stripplot with error bars, preferably in Lattice. Let's call this a
TIE fighter plot. Any suggestions?

Cheers,
Shaun

On 4 July 2013 18:00, Dennis Murphy <djm...@gmail.com> wrote:

> If you consult the lattice package help, you'll discover there is no
> panel_errorbar() function, which would imply the package developers
> have a distaste for that type of graphic. If you fish around the
> R-help archives, though, you might be able to find someone who wrote a
> function to do error bars in lattice. (Use a searchable archive such
> as Nabble to hunt for it.)
>
> Error bar plots are easier to do in the ggplot2 package, since there
> is a specific function to generate the error bar 'geometry'
> (geom_errorbar). See http://docs.ggplot2.org/current/ for an expanded
> version of the package help pages, which include the graphs generated
> by the code. I believe there's also a base graphics version that you
> can get from the gplots package, but I don't know a lot about it.
>
> Dennis
>
> On Thu, Jul 4, 2013 at 2:53 PM, Shaun Jackman <sjac...@gmail.com> wrote:
[[alternative HTML version deleted]]

David Winsemius

unread,
Jul 5, 2013, 2:28:35 PM7/5/13
to Shaun Jackman, R help, Bert Gunter

On Jul 5, 2013, at 11:15 AM, Shaun Jackman wrote:

> Hi Bert, Dennis,
>
> I'll agree that using a barchart was a poor choice. I was in fact using a
> notched bwplot to show the median and confidence interval of the median. In
> this case it's the median and confidence interval that I want to highlight,
> and I find that the visual noise of the box and whiskers is detracting from
> the focus, and those wee notches are not much to focus on. So, I'd like to
> draw a stripplot with error bars, preferably in Lattice. Let's call this a
> TIE fighter plot. Any suggestions?
>

I like the TIE fighter label. Try this:

library(latticeExtra)
data(USCancerRates)
segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male,
data = subset(USCancerRates, state == "Washington"),
draw.bands = FALSE, centers = rate.male,
segments.fun = panel.arrows, ends = "both",
angle = 90, length = 1, unit = "mm")

It's what Sarkar has recommended in the past when this request has been posted.

--
David
David Winsemius
Alameda, CA, USA

Bert Gunter

unread,
Jul 5, 2013, 2:48:46 PM7/5/13
to David Winsemius, R help
Be careful!

You are talking about 2 different varieties of apples here. As I read
it, the CI's in the cancer data, which I know is just for example
purposes, are CI's for the **individual means**; the notches in
boxplots are nonparametric and for 2 groups with roughly equal sample
sizes, "The idea appears to be to give roughly a 95% confidence
interval for the **difference** in two medians." (from
?boxplot.stats). So I'm not sure which you want, but they are
certainly different (by a factor of around sqrt(2),right?), even if
both are for the mean or both are for the median.

Cheers,
Bert
--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

Shaun Jackman

unread,
Jul 5, 2013, 3:11:29 PM7/5/13
to David Winsemius, R help, Bert Gunter
Yes! Thank you, David. That's exactly what I'm I'm looking for. For
the record, here's a couple pages leading to this answer:

http://www.hep.by/gnu/r-patched/r-faq/R-FAQ_89.html
http://latticeextra.r-forge.r-project.org/man/segplot.html
http://rgm3.lab.nig.ac.jp/RGM/r_function?p=latticeExtra&f=segplot

For a related question, what's the tidiest way to calculate the
medians and confidence intervals? Currently I'm using `boxplot`:

require(datasets)
ci <- with(boxplot(weight ~ Diet, ChickWeight), data.frame(
Diet = names,
median = stats[3,],
lower = conf[1,],
upper = conf[2,]))

Cheers,
Shaun

Shaun Jackman

unread,
Jul 5, 2013, 3:15:13 PM7/5/13
to Bert Gunter, R help
Hmm. Interesting point, Bert. I don't know whether the notches show
the 95% confidence interval or the median, or the 95% confidence
interval that two non-overlapping notches have different medians.
You're saying it's the latter? Anyone know what the 95% confidence
interval of the median would be?

Cheers,
Shaun

> The notches (if requested) extend to +/-1.58 IQR/sqrt(n). This seems to be based on the same calculations as the formula with 1.57 in Chambers et al. (1983, p. 62), given in McGill et al. (1978, p. 16). They are based on asymptotic normality of the median and roughly equal sample sizes for the two medians being compared, and are said to be rather insensitive to the underlying distributions of the samples. The idea appears to be to give roughly a 95% confidence interval for the difference in two medians.

Bert Gunter

unread,
Jul 5, 2013, 5:30:57 PM7/5/13
to Shaun Jackman, R help, Bert Gunter
This is not an R question. Read the references.

Bert

Sent from my iPhone -- please excuse typos.

S Ellison

unread,
Jul 8, 2013, 1:28:35 PM7/8/13
to R help
> > Anyone know what the 95% confidence
> > interval of the median would be?
For an R answer you could get one for each group from wilcox.test( .... , conf.int =TRUE ) and build that into an alternative boxplot stats function which you could specify in your bwplot call.

Steve Ellison


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
Reply all
Reply to author
Forward
0 new messages