[R] barchart() {Lattice} help.

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Nov 27 13:21:26 CET 2009


On Fri, Nov 27, 2009 at 9:21 AM, Peng Cai <pengcaimaillist at gmail.com> wrote:
> @ Peter: I got it, thanks a lot for all your help! And yes, as you said the
> "title" option in auto.key is redundant.
>
>
> @ All, Hi: I need to add percentage sign to y-axis labels (like 0%, 20%,
> ..., 100%). How can I get it. I'm using barchart() function as given below
> along with the data set.

Read ?barchart, specifically the entry for 'scales'. Hint: figure out
what the line

     scales = list(y = list(at = myYscale)),

does.

-Deepayan


>
> Data:
> Sample    Col1    Col2    Col3
> Row1    -20    40    -10
> Row2    30    -20    40
> Row3    30    10    -20
> Row4    20    20    -10
>
> R code:
>
> library(lattice)
> dta<-read.table("data.txt", header=TRUE, row.names="Sample")
> myYscale <- seq(-140,140,20)
> barchart(data.matrix(dta),
>    horizontal=FALSE,
>      stack=TRUE,
>    par.settings = simpleTheme(col = c(2:4)),
>    auto.key=list(space = 'right', rows = 3, rectangles = TRUE,
>                  points = FALSE ),
>    scales = list(y = list(at = myYscale)),
>    panel=function(x,y,...){
>        panel.abline(h=c(myYscale), col.line="gray")
>          panel.barchart(x,y,...)}
> )
>
> Any help would be greatly appreciated,
> Peng
>
> On Thu, Nov 26, 2009 at 10:38 PM, P Ehlers <ehlers at ucalgary.ca> wrote:
>
>> As I wrote earlier:
>>
>>
>> "I had to add the rectangles= and points= arguments to
>>
>> auto.key to get the same key as you had earlier."
>>
>> and the relevant line in the code was:
>>
>>
>>  auto.key = list(space = 'right', rectangles=TRUE, points=FALSE)
>>
>> -Peter Ehlers
>>
>> Peng Cai wrote:
>>
>>> Hello Peter and David,
>>>
>>> Thanks for your help. I have added what you suggested and its working
>>> perfectly fine except:
>>>
>>> When I add the panel function, the legend changes. In the sense without
>>> the
>>> panel function the column names are shown with small colored rectangles
>>> (on
>>> right), but after adding it the rectangles change to tiny un-filled
>>> diamonds. Any suggestions?
>>>
>>> My current code and data is below,
>>>
>>> Thanks a lot,
>>> Peng
>>>
>>>
>>> Data:
>>>
>>>
>>>> Sample Col1 Col2 Col3
>>>> Row1 -2 4 -1
>>>> Row2 3 -2 4
>>>> Row3 3 5 -2
>>>> Row4 4 1 -1
>>>>
>>>> Code:
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>> dta<-read.table("data.txt", header=TRUE, row.names="Sample")
>>>> coltemp=c(619,376,497)
>>>> myYscale <- seq(-10, 10, 1)
>>>> barchart(data.matrix(dta),
>>>>       horizontal=FALSE,
>>>>       stack=TRUE,
>>>>     par.settings = simpleTheme(col = colors()[coltemp]),
>>>>       auto.key=list(space="right"),
>>>>     border=NA,
>>>>     panel=function(x,y,...){
>>>>              panel.abline(h=c(myYscale), col.line="gray")
>>>>              panel.barchart(x,y,...)
>>>>       },
>>>>     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
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>
>>>
>>
>
>        [[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