[R] Lattice barchart with error bars

Shaun Jackman sjackman at gmail.com
Fri Jul 5 21:11:29 CEST 2013


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

On 5 July 2013 11:28, David Winsemius <dwinsemius at comcast.net> wrote:
>
> 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
>
>
>> Cheers,
>> Shaun
>>
>> On 4 July 2013 18:00, Dennis Murphy <djmuser at 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 <sjackman at gmail.com> wrote:
>>>> 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-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.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
> David Winsemius
> Alameda, CA, USA
>



More information about the R-help mailing list