Skip to content
Prev 261200 / 398502 Next

help with barplot

Hi Steven,

This is not, strictly speaking, the answer to your question (hopefully
Tom already answered that).  Rather, it is the answer to questions you
*might* have asked (and perhaps one of them will be one you wished you
had asked).

Barplots have a low data:ink ratio...you are using an entire plot to
convey 8 means.  A variety of alternatives exist.  As a minimal first
step, you could just use points to show the means and skip all the
wasted bar space, and you might add error bars in (A).  You could also
use boxplots to give your viewers (or just yourself) a sense of the
distribution along with the medians (B).  Another elegant option is
violin plots.  These are kind of like (exactly like?) mirrored density
plots.  A measure of central tendency is not explicitly shown, but the
*entire* distribution and range is shown (C).

Cheers,

Josh

(P.S. I hit send too soon before and sent you an offlist message with
PDF examples)

## Create your data
DF <- data.frame(
  Incidents = factor(rep(c("a", "b", "d", "e"), each = 25)),
  Months = factor(rep(1:2, each = 10)),
  Time = rnorm(100))

## Load required packages
require(ggplot2)
require(Hmisc)

## Option A
ggplot(DF, aes(x = Incidents, y = Time, colour = Months)) +
  stat_summary(fun.y = "mean", geom = "point",
    position = position_dodge(width = .90), size = 3) +
  stat_summary(fun.data = "mean_cl_normal", geom = "errorbar",
    position = "dodge")

## Option B
ggplot(DF, aes(x = Incidents, y = Time, fill = Months)) +
  geom_boxplot(position = position_dodge(width = .8))

## Option C
ggplot(DF, aes(x = Time, fill = Months)) +
  geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
    alpha = .2, stat = "density") +
  facet_grid( ~ Incidents) +
  coord_flip()

## Option C altered
ggplot(DF, aes(x = Time, fill = Months)) +
  geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
    alpha = .2, stat = "density") +
  facet_grid( ~ Incidents + Months) +
  scale_y_continuous(name = "density", breaks = NA, labels = NA) +
  coord_flip()
On Fri, May 27, 2011 at 3:08 PM, steven mosher <moshersteven at gmail.com> wrote: