[R] adding lines to a barchart

Spilak,Jacqueline [Edm] Jacqueline.Spilak at EC.gc.ca
Fri Dec 21 20:00:23 CET 2007


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?
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?
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