[R] barchart() {lattice} help.

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Dec 6 16:08:13 CET 2009



Peng Cai wrote:
> Hi,
> 
> I'm plotting grouped barplot using following code and data. I need help with
> re-ordering the labels.
> 
> 1. On x-axis the factor "AGE" is grouped in order "0--4", "15--18", "5--14";
> whereas I would like to have it in "0--4", "5--14", "15--18".
> 
> 2. If I need to re-order "RACE" variable. How can I do it assuming I need to
> change it on both the x-axis and legend. Currenlty the order is
> "Black","Other","White"; whereas I would like "White", "Black", "Other".
> 
> Can anyone help please. I'm using following code, which is working fine
> except above issues.
> 
> Code:
> 
> library(lattice)

To answer your question:

  dta$age <- factor(dta$age, levels = c("0--4", "5--14", "15--18"),
                    ordered = TRUE)
  dta$race <- factor(dta$race, levels = c("White", "Black", "Other"))

  library(lattice)
  barchart(sum ~ age | gender, data = dta, groups = race,
    stack = FALSE,
    ylab = "Sum of admissions over 10 years (1996-2005)",
    xlab = "Age",
    par.settings = simpleTheme(col = c("green1", "yellow1", "orange")),
    key = list(space="right", cex=1, text=list(levels(dta$race)),
               rectangles=list(size=1.7, border="white",
                               col = c("green1", "yellow1", "orange"))),
    strip = strip.custom(bg="greenyellow")
)




> ################### assuming data is read in object name "dta".
> attach(dta)


Other comments:

1. You do not need attach at all here - and I recommend not to use it 
unless you really know what you are doing and you really need it.
2. you might want to choose more appropriate colours.

Best,
Uwe Ligges


> barchart(sum ~ age | gender, data = dta,
>             groups = race,
>   stack = FALSE,
>   ylab="Sum of admissions over 10 years (1996-2005)",
>   xlab="Age",
>   par.settings=simpleTheme(col = c("green1", "yellow1", "orange")),
>   key=list(space="right", cex=1,
>   text=list(c("Black","Other","White")),
>   rectangles=list(size=1.7, border="white", col = c("green1", "yellow1",
> "orange"))),
>   strip = strip.custom(bg="greenyellow")
> )
> detach(dta)
> 
> Data:
> age gender  race sum
> 0--4 Female Black 145
> 0--4 Female Other  53
> 0--4 Female White  47
> 0--4   Male Black 286
> 0--4   Male Other 130
> 0--4   Male White  94
> 15--18 Female Black  30
> 15--18 Female Other   3
> 15--18 Female White   9
> 15--18   Male Black  21
> 15--18   Male Other   2
> 15--18   Male White   3
> 5--14 Female Black 138
> 5--14 Female Other  31
> 5--14 Female White  23
> 5--14   Male Black 199
> 5--14   Male Other  65
> 5--14   Male White  29
> 
> Thanks,
> Peng
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list