[R] barchart() {Lattice} help.

Peter Ehlers ehlers at ucalgary.ca
Fri Nov 27 02:09:24 CET 2009


Peng,

Did you try the code I sent? If not, why not?
If you want equally spaced grid lines, use panel.grid() in
place of panel.abline().

BTW, I don't understand what that "title=..." stuff in
your auto.key() call is supposed to do. I think it just
results in title=NULL.

  -Peter Ehlers


Peng Cai wrote:
> Currently I'm trying and I'm looking to draw lines y=-3, y=-2,..., y=8:
> 
> Data:
> Sample Col1 Col2 Col3
> Row1 -2 4 -1
> Row2 3 -2 4
> Row3 3 5 -2
> Row4 4 1 -1
> 
> Code:
> library(lattice)
> dta<-read.table("data.txt", header=TRUE, row.names="Sample")
> coltemp=c(619,376,497,598,124,92,402)
> myYscale <- seq(-10, 10, 1)
> barchart(data.matrix(dta),
>        horizontal=FALSE,
>        stack=TRUE,
>      par.settings = simpleTheme(col = colors()[coltemp]),
>        auto.key=list(space="right",
>      title=names(dimnames(dta))[2]),
>      border=NA,
>      main="NextGen - 2025 Monthly Average PM2.5 Contribution at ATL",
>      ylab="Speciated PM2.5 Contribution (µg/m3)",
>      xlab="Month",
>      box.ratio=2.5,
>      scales = list(y = list(at = myYscale)),
> )
> 
> Thanks,
> Peng
> 
> On Thu, Nov 26, 2009 at 7:23 PM, Peng Cai <pengcaimaillist at gmail.com> wrote:
> 
>> Hi Again,
>>
>> Before I start getting into what you just suggested, let me confirm if I
>> made my point clear previously. I'm looking for horizontal lines similar to
>> one on the following link (It has parallel lines for each y=200, y=400,...):
>>
>> http://pfiles.5min.com/images/176735/176734313.jpg
>>
>> What you just suggested can solve this purpose? Thanks,
>>
>> Peng
>>
>>
>> On Thu, Nov 26, 2009 at 7:09 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
>>
>>> Peng Cai wrote:
>>>
>>>> Thanks David, I tried panel.abline(h=somevalue) -- both inside and
>>>> outside
>>>> of barchart() function but its not working. Any suggestions?
>>>>
>>>> Peng
>>>>
>>> Here's some code related to the data you posted earlier.
>>>
>>>
>>> barchart(data.matrix(dta), horizontal = FALSE, stack = TRUE,
>>>             par.settings = simpleTheme(col = 2:4),
>>>             panel=function(x,y,...){
>>>               panel.abline(h=c(-2,0,3,4), col.line="gray")
>>>               panel.barchart(x,y,...)
>>>
>>>             },
>>>             scales = list(y = list(at = -2:8)),
>>>             auto.key = list(space = 'right', rectangles=TRUE,
>>>                 points=FALSE)
>>> )
>>>
>>> If you want the gray lines in front of the bars, switch the
>>> order of the panel functions. With lattice, it's all about
>>> what goes into each panel (you have only one panel here).
>>> If you want more than one thing in a panel, you have to set
>>> up a function to do those things.
>>>
>>> I had to add the rectangles= and points= arguments to
>>> auto.key to get the same key as you had earlier.
>>>
>>>  -Peter Ehlers
>>>
>>>
>>>
>>>> On Thu, Nov 26, 2009 at 6:42 PM, David Winsemius <dwinsemius at comcast.net
>>>>> wrote:
>>>>  On Nov 26, 2009, at 6:12 PM, Peng Cai wrote:
>>>>>  Thanks a lot Peter! One more help, is there a similar function abline()
>>>>>
>>>>>> for
>>>>>> barchart().
>>>>>>
>>>>>>  ?panel.abline
>>>>>
>>>>>
>>>>>  I'm trying to add a (light gray colored) horizontal lines, one for each
>>>>>> y-value.
>>>>>>
>>>>>> Peng
>>>>>>
>>>>>> On Thu, Nov 26, 2009 at 5:59 PM, Peter Ehlers <ehlers at ucalgary.ca>
>>>>>> wrote:
>>>>>>
>>>>>>  Peng Cai wrote:
>>>>>>
>>>>>>>  Hi Peter,
>>>>>>>
>>>>>>>> I'm not sure but it seems "scales" command works only with integer
>>>>>>>> values.
>>>>>>>>
>>>>>>>> If the y-axis values are very small (such as -0.03, -0.02, -0.01, 0,
>>>>>>>> 0.01,..., 0.08). My current plot has values 0, 0.05, and 0.10 only.
>>>>>>>> But
>>>>>>>> I
>>>>>>>> need it to extend it to negative numbers and reduce the scale width
>>>>>>>> (like
>>>>>>>> -0.04, -0.02, 0, 0.02,...).
>>>>>>>>
>>>>>>>> Can I change these too? Thanks!
>>>>>>>>
>>>>>>>>
>>>>>>>>  Use, e.g.
>>>>>>> myYscale <- seq(-0.04, 0.08, 0.02)
>>>>>>> barchart(...,
>>>>>>>  ...,
>>>>>>>  scales = list(y = list(at = myYscale)),
>>>>>>>  ...
>>>>>>> )
>>>>>>>
>>>>>>> -Peter Ehlers
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>  Peng
>>>>>>>
>>>>>>>> On Thu, Nov 26, 2009 at 3:18 PM, Peter Ehlers <ehlers at ucalgary.ca>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>  Peng Cai wrote:
>>>>>>>>
>>>>>>>>> Hi R Users,
>>>>>>>>>
>>>>>>>>>  I'm trying to plot a stacked barplot. Here is data:
>>>>>>>>>> Sample Col1 Col2 Col3
>>>>>>>>>> Row1 -2 4 -1
>>>>>>>>>> Row2 3 -2 4
>>>>>>>>>> Row3 3 5 -2
>>>>>>>>>> Row4 4 1 -1
>>>>>>>>>>
>>>>>>>>>> I'm using following R code:
>>>>>>>>>>
>>>>>>>>>> library(lattice)
>>>>>>>>>> dta<-read.table("data.txt", header=TRUE, row.names="Sample")
>>>>>>>>>> barchart(data.matrix(dta),
>>>>>>>>>>    horizontal=FALSE,
>>>>>>>>>>    stack=TRUE,
>>>>>>>>>> col=2:4,
>>>>>>>>>>    auto.key=list(space="right",
>>>>>>>>>> title=names(dimnames(dta))[2])
>>>>>>>>>> )
>>>>>>>>>>
>>>>>>>>>> Above code is working fine, but I need help with:
>>>>>>>>>>
>>>>>>>>>> 1) Legend boxes have default colors, whereas I'm looking them to
>>>>>>>>>> match
>>>>>>>>>> with
>>>>>>>>>> barplot colors (col=2:4).
>>>>>>>>>>
>>>>>>>>>> replace the line
>>>>>>>>>>
>>>>>>>>>>  col = 2:4,
>>>>>>>>> with
>>>>>>>>>
>>>>>>>>> par.settings = simpleTheme(col = 2:4),
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> 2) Can I increase scale for y axis, like currently it plotting
>>>>>>>>>
>>>>>>>>>  -2,0,2,4,...
>>>>>>>>>
>>>>>>>>>> I would like it as -2,-1,0,1,...
>>>>>>>>>>
>>>>>>>>>> add the line
>>>>>>>>>>
>>>>>>>>>>  scales = list(y = list(at = -2:8)),
>>>>>>>>> or whatever tick locations you prefer.
>>>>>>>>>
>>>>>>>>> -Peter Ehlers
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Any help would be greatly appreciated,
>>>>>>>>>
>>>>>>>>>  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.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>   ______________________________________________
>>>>>>> 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.
>>>>>>>
>>>>>>>
>>>>>>>        [[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.
>>>>>>
>>>>>>  David Winsemius, MD
>>>>> Heritage Laboratories
>>>>> West Hartford, CT
>>>>>
>>>>>
>>>>>
>




More information about the R-help mailing list