[R] Barplot by two variables

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri May 11 00:58:10 CEST 2007

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:

barchart(HSS ~ factor(Year), data = dataset, origin = 0,
         groups = Limit, auto.key = TRUE)


More information about the R-help mailing list