# Re: [R] help with barplot

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Fri, 27 May 2011 22:29:44 -0700

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)

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

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:
> Hi,
>
> I'm really struggling with barplot
>
> I have a data.frame with 3 columns. The first column represents an
> "incident" type
> The second column represents a "month"
> The third column represents a "time"
>
> Code for a sample data.frame
>
> incidents <- rep(c('a','b','d','e'), each =25)
>  months    <- rep(c(1,2), each =10)
>  times     <-rnorm(100)
>
> #  make my sample data
>
>  DF        <-
> data.frame(Incidents=as.factor(incidents),Months=as.factor(months),Time=times)
>
> # now calculate a mean for the  "by" groups of incident type and month
>
>  pivot <-
> aggregate(DF\$Time,by=list(Incidents=DF\$Incidents,Months=DF\$Month),FUN=mean,simplify=TRUE)
>
> What I want to create is a bar plot where  I have groupings by incident type
> ( a,b,d,e) and within each group
> I have the months in order.
>
> So group 1 would  be  Type "a"; month 1,2;
>     group 2 would  be  Type "b"; month 1,2;
>     group 3 would  be  Type "d"; month 1,2;
>    group 4 would  be  Type "3"; month 1,2;
>
> I know barplot is probably the right function but I'm a bit lost on how to
> specify groupings etc
>
> TIA
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

```--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help