[R] adding lines to a barchart

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Dec 21 20:31:18 CET 2007


On 12/21/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> Thanks Deepayan, this helped a lot and gave me exactly what I wanted
> however I want a few changes and am not sure how to do them.  In each
> panel the bars for the years are the same color with the bottom axis
> (the x-axis) labelled with the years.  I would like each year to have a
> specific color with a legend for it at the side, is this possible?

Sure, just use Year as a grouping variable as well.

barchart(percent_below ~ factor(Year) | factor(season, levels=unique(season)),
         data = dataset1, layout = c(4, 1),
         groups = factor(Year),
         auto.key = list(points = FALSE, rectangles = TRUE),
         stack = TRUE,
         upper_2007 = c(6, 4, 5, 3),
         panel = function(..., upper_2007) {
             panel.abline(h = upper_2007[packet.number()])
             panel.barchart(...)
         })


> And my other question is that there seems to be a gap between where the
> bars start and the x-axis, it is kinda funny looking so is there anyway
> to change this?

What exactly do you want to happen? I don't think a bar chart is an
appropriate visualization unless the lengths of the bars mean
something. If it does, and you just want to get rid of the space below
zero, the easiest way is to set an explicit ylim=c(0, 7) or something.
If the lengths don't really mean anything, you should consider using
dotplot() instead.

-Deepayan

> Thanks for all the help
>
> On 12/19/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> > Hi all
> > I can't find what I am looking for so I am asking here.  I have a
> > dataset that looks something like this.
> >
> > Year   season  percent_below
> > 2000 Winter     6.9179870
> > 2000 Spring     1.6829436
> > 2000 Summer     1.8463501
> > 2000 Autumn     3.8184993
> > 2001 Winter     2.8832806
> > 2001 Spring     2.5870511
> > 2001 Summer     0.0000000
> > 2001 Autumn     4.7248240
> > 2002 Winter     4.4532238
> > 2002 Spring     3.7468846
> > 2002 Summer     1.0784311
> > 2002 Autumn     3.7061533
> >
> > I have plotted this nicely using
> > barchart(percent_below ~ factor(Season, levels=Season), data=
> > dataset1,
> > group=Year)
> > This gives me a barplot by season with each year shown in that season.
> > Now the tricky part.  My data is from 2000 to 2007 and 2007 is an
> > important year.  I have an upper and lower limit for percent_below for
> > 2007 for each season.  I would like to add the upper and lower limit
> > of
> > 2007 as a black line to each season.  The upper and lower limit will
> > be different for each season.  This is being done so for a comparison
> > so I need it done this way.
>
> I would suggest a slightly different format. Here's an example with one
> set of limits; you can add another set similarly. If you really want a
> single panel, you could use panel.segments().
>
> barchart(percent_below ~ factor(Year) | factor(season,
> levels=unique(season)),
>          data= dataset1, origin = 0, layout = c(4, 1),
>          upper_2007 = c(6, 4, 5, 3),
>          panel = function(..., upper_2007) {
>              panel.abline(h = upper_2007[packet.number()])
>              panel.barchart(...)
>          })
>
> -Deepayan
>



More information about the R-help mailing list