# [R] Barplot by two variables

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue May 15 23:43:13 CEST 2007

```On 5/15/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
>
>  Thanks for your solution, it worked perfectly, it was exactly what I
> wanted.  I do have two more questions and hope you can help.  I have
> another analysis exactly like the last one except it is done by month
> instead of year.  When I graph it using barchart it makes the months go
> in alphabetical order.  Is there anyway to change it so that the months
> go in the correct order (jan, feb, march, etc,).  And how do I change
> the colors of the bars in the graph, they are weird colors and I want to
> change them.

It would help to have a reproducible example. The alphabetical order
thing is a side effect of the factor() function, and to avoid it you
need to supply the levels when you create the factor; e.g.

> factor(month.name)
[1] January   February  March     April     May       June      July
[8] August    September October   November  December
12 Levels: April August December February January July June March ... September
>
> factor(month.name, levels = month.name)
[1] January   February  March     April     May       June      July
[8] August    September October   November  December
12 Levels: January February March April May June July August ... December

(Note the different order of the levels)

As for the colors, you can add either

col=c("green","purple")

or

par.settings = list(superpose.polygon = list(col=c("green","purple")))

The first will change the color of the bars but not the legend, the
second will change both. (The reasons this works are a bit convoluted
if you are not familiar with lattice, and you don't really need to
know, so I won't go into them)

-Deepayan

> Thanks so much for your help.
>
> -----Original Message-----
> From: Deepayan Sarkar [mailto:deepayan.sarkar at gmail.com]
> Sent: May 10, 2007 4:58 PM
> To: Spilak,Jacqueline [Edm]
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Barplot by two variables
>
> On 5/10/07, Spilak,Jacqueline [Edm] <Jacqueline.Spilak at ec.gc.ca> wrote:
> > Hi all
> > I have a bit of a problem.  I want to make a barplot of some data.  My
>
> > data is of a score that is separated by year and by a limit (above 3
> > and below 3 to calculate the score).
> > Year    Limit   HSS
> > 1999    ALT     0.675
> > 1999    VFR     0.521
> > 2000    ALT     0.264
> > 2000    VFR     0.295
> >
> > I would like to have a barplot with year on the x axis and HSS on the
> > y axis and the two limits as two different colors to show the
> difference.
> > Using (dataset\$HSS, col=c("green","purple"))  I  get some of the plot
> > but I don't know how to get labels on the bottom for each year and I
> > can't get a legend for my barplot.  Not really sure what I am doing
> > wrong but any help would be much appreciated.
>
> Here's one solution using the lattice package:
>
> library(lattice)
> barchart(HSS ~ factor(Year), data = dataset, origin = 0,
>          groups = Limit, auto.key = TRUE)
>
> -Deepayan
>

```