Re: [R] Barplot by two variables

From: Deepayan Sarkar <>
Date: Tue, 15 May 2007 14:43:13 -0700

On 5/15/07, Spilak,Jacqueline [Edm] <> 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(

 [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(, levels =
 [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



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)


> Thanks so much for your help.
> -----Original Message-----
> From: Deepayan Sarkar []
> Sent: May 10, 2007 4:58 PM
> To: Spilak,Jacqueline [Edm]
> Cc:
> Subject: Re: [R] Barplot by two variables
> On 5/10/07, Spilak,Jacqueline [Edm] <> 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
> mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 15 May 2007 - 21:49:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 May 2007 - 22:31:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.