[R] Bar width and labels in barchart

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Jan 26 01:43:11 CET 2008


On 1/25/08, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
>
> -----Original Message-----
> From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com]
> Sent: Thursday, January 24, 2008 9:56 AM
> To: Spilak,Jacqueline [Edm]
> Cc: r-help at r-project.org
> Subject: *****SPAM***** Re: [R] Bar width and labels in barchart
>
> On 1/23/08, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> >> Hi everyone
> >> I am using barchart to make my graphs.  Here is my code.
> >>
> >> barchart(percent_below ~ factor(Year)| factor(Season,
> >> levels=unique(Season)),
> >>       data= .season_occurrence, origin = 0, layout = c(4, 1),
> >> scales=list(tick.number=ticknum,labels=NULL), ylim=c(0, ymax),
> >>         group = factor(Year), xlab= "Year", auto.key = list(points =
> >> FALSE, rectangles = TRUE,space="right",size=2,cex=0.8),
> >>         upper_2007 = c(upper_limit_winter, upper_limit_spring,
> >> upper_limit_summer, upper_limit_autumn),
> >>         par.settings = list(superpose.polygon =
> >>
> list(col=c(color1,color2,color3,color4,color5,color6,color7,color8))),
> >>         lower_2007 = c(lower_limit_winter, lower_limit_spring,
> >> lower_limit_summer, lower_limit_autumn),
> >>       panel = function(..., upper_2007,lower_2007) {
> >>             panel.abline(h = upper_2007[packet.number()])
> >>                 panel.abline(h = lower_2007[packet.number()])
> >>             panel.barchart(...)
> >>          })
> >>
> >> This code gives me a graph with four panels, one for each season, and
>
> >> then the percent_below is shown per year in each panel.  I wanted
> each
> >> year to have a color associated with it so I added group =
> >> factor(Year) this worked and gave me what I wanted, however now the
> >> bars are very skinny and it is hard to distinguish the colors.  How
> do
> >> I make them wider?  My other question is that on the x-axis it list
> >> all years (2000-2007, inclusively) for each panel and they are
> >> squished and non-legable, can I get rid of them?  They are not
> >> necessary as I have the years color coded with the legend at the
> side.
>
> >Does having stack=TRUE help?
>
> >If you have year as the x-variable, barchart will try to label them.
> >You could control the labels using 'scales', but I think having some
> other variable >as the x-variable might be the better solution. We need
> a (minimal) reproducible >example to give more definite advice.
>
> >-Deepayan
>
> Sorry, I completely forgot about an example, so here one is but the
> actually data does go up to 2007
>
> 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
>
> I tried adding stack=TRUE but it didn't do anything, so I am not sure if
> I am doing something wrong with the coding. I also tried controling the
> labels using scales=list(labels=NULL) but it didn't do anything, so not
> sure what I did wrong.  I know that I could have seasons as the
> x-variable however in my graph I want each season vs. percent_below by
> each year for each season.  I also added a panel because I neede to add
> a line to each season to show an upper and lower limit and they were
> different for each season.  I have tried barchart(percent_below ~
> factor(Season), groups=Year) but it doesn't give me what I want.  So
> still not sure how to get rid of the x-axis labels and how to make the
> bar widths wider.

I would suggest creating a dummy variable (a 1-level factor) for the x-axis:

barchart(percent_below ~ gl(1, length(Year)) | factor(Season,
levels=unique(Season)),
         data= .season_occurrence, origin = 0, layout = c(4, 1),
         group = factor(Year),
         scales = list(x = list(draw = FALSE)),
         auto.key = list(points = FALSE, rectangles =
TRUE,space="right",size=2,cex=0.8))

-Deepayan



More information about the R-help mailing list