[R] barchart() {Lattice} help.

Peter Ehlers ehlers at ucalgary.ca
Fri Nov 27 01:09:17 CET 2009


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