[R] barchart() {lattice} help.

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Dec 9 07:45:19 CET 2009


On Sun, Dec 6, 2009 at 8:53 AM, Peng Cai <pengcaimaillist at gmail.com> wrote:
> Please ignore my last question. I found a way to handle that. One last
> thing:
>
> I'm defining my own y scales. In the process the bar starts from below the
> "y=0" line (or below the y-axis). Is there a way to get rid of it.

Have you tried adding 'origin=0' ?

-Deepayan

>
> Here is the code and data I'm using
>  R Code (Data read in object "dta")
>
> 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"))
> *yScale<-seq(0,1000,50)
> *library(lattice)
> barchart(dta$sum ~ dta$age | dta$gender,
>  data = dta,
>  groups = dta$race,
>  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"),
> * scales=list(y=list(rot=360, at=yScale), tck=c(1,0)),
>  panel=function(x,y,...)
>  {
>  panel.abline(h=c(yScale), col.line="gray")
>            panel.barchart(x,y,...)
>  }
> *)
>
>  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
>
>
> On Sun, Dec 6, 2009 at 11:47 AM, Peng Cai <pengcaimaillist at gmail.com> wrote:
>
>> Thank you Uwe, Dennis, and Gary for your help. I have one more question:
>>
>> I'm using pre-defined y-scales and trying to create grid lines.
>>
>> As "Female" category has low sum value, its y-axis range from 0-150 whereas
>> "Male" ranges from 0-300. Is it possible to make them on same scale. Here is
>> the previous code with an additional yScale code.
>>
>> R Code (Data read in object "dta")
>>
>> 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"))
>> *yScale<-seq(0,1000,50)
>> *library(lattice)
>> barchart(dta$sum ~ dta$age | dta$gender,
>>  data = dta,
>>  groups = dta$race,
>>  stack = FALSE,
>>  aspect=0.6,
>>  layout=c(2,1),
>>
>>  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"),
>> * scales=list(relation="free", y=list(rot=360, at=yScale)),
>>  panel=function(x,y,...)
>>  {
>>   panel.abline(h=c(yScale), col.line="gray")
>>             panel.barchart(x,y,...)
>>  }  *
>> )
>>
>>
>> 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
>>
>>   On Sun, Dec 6, 2009 at 11:18 AM, Gary Miller <mail2garymiller at gmail.com>wrote:
>>
>>> Thanks Uwe, I got your suggestions part too.
>>>
>>> 2009/12/6 Uwe Ligges <ligges at statistik.tu-dortmund.de>
>>>
>>> >
>>> >
>>> > 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<http://www.r-project.org/posting-guide.html>
>>> <http://www.r-project.org/posting-guide.html>
>>> >> and provide commented, minimal, self-contained, reproducible code.
>>> >>
>>> >
>>> > ______________________________________________
>>> > 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<http://www.r-project.org/posting-guide.html>
>>> <http://www.r-project.org/posting-guide.html>
>>>  > and provide commented, minimal, self-contained, reproducible code.
>>> >
>>>
>>>        [[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<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
>        [[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