Labels for Bars in R Lattice Bar Chart

864 views Asked by At

I have a dataset that includes a "Gender" element. That is coded 1 for male and 2 for female. I'm trying to create a simple bar chart using lattice in R to compare the number of males and females. This is the code I am trying:

> barchart(equal.count(Gender),
+ horizontal=FALSE,
+ names.arg = c("Male","Female"))

This code does, in fact, create a bar chart with the correct heights for each bar. However, the labels for the bars are "1" and "2"; and I would like to use something more meaningful, like "Male" and "Female".

I've tried several variations on line #3 above, including; key.legend=c("Male","Female") -- key.title=c("Male","Female") -- key.title=list("Male","Female") -- labels=list("Male","Female"). It seems that no matter what I do the chart still shows "1" and "2" as labels for the two bars. How can I create a bar chart where the labels are more meaningful than "1" and "2"? Thanks for any help you can offer.

1

There are 1 answers

2
LyzandeR On BEST ANSWER

You need the xlab argument:

library(lattice)

Gender <- rep(c(1,2),50)

barchart(equal.count(Gender),
          horizontal=FALSE,
          xlab = c("Male","Female"))

enter image description here