[R] Adding values to top of bars in barchart

Eric Archer Eric.Archer at noaa.gov
Fri Apr 14 21:53:49 CEST 2006


Deepayan,

Thanks much!  That works perfectly!

Cheers,
eric

Deepayan Sarkar wrote:
> On 4/14/06, Eric Archer <Eric.Archer at noaa.gov> wrote:
>   
>> Given the following data frame ("freq.sp"),
>>
>>  > str(freq.sp)
>> `data.frame':   42 obs. of  4 variables:
>>  $ behav  : Factor w/ 6 levels "approach","bowride",..: 1 1 1 1 1 1 1 2
>> 2 2 ...
>>  $ species: Factor w/ 7 levels "COAST_SPOT","EAST_SPINR",..: 1 2 3 4 5 6
>> 7 1 2 3 ...
>>  $ n      : int  193 194 563 357 570 369 74 194 208 633 ...
>>  $ pct    : num  0.725 0.340 0.252 0.381 0.072 ...
>>
>> I create a trellis barchart with the following command,
>>
>> barchart(pct ~ behav | species, data=freq.sp, as.table=TRUE,
>> xlab="Behavior",
>>     ylab="Frequency", ylim=c(0,1), main="Frequencies of Behaviors",
>>     scales=list(x=list(rot=45)))
>>
>> In this graph, I would like to include the sample sizes (the value of
>> freq.sp$n corresponding to each freq.sp$pct) at the top of each bar.  I
>> don't see a specific command in barchart that will allow me to do this
>> and am fairly new to lattice graphics.  I've done RSiteSearches on
>> keywords that I could think of, but didn't run across anything I
>> recognized as useful. Any pointers on how to accomplish this would be
>> greatly appreciated.  Thanks in advance.
>>     
>
> Here's an example using the barley data. For more flexible placement
> of the text, use grid.text from the grid package directly (instead of
> panel.text):
>
>
> library(lattice)
>
> barchart(I(yield / sum(yield)) ~ variety | site, barley,
>          subset = (year == "1931"),
>          scales = list(x = list(rot = 45)),
>          label = round(barley$yield),
>          panel = function(x, y, label, subscripts, ...) {
>              lab <- label[subscripts]
>              panel.barchart(x, y, ..., subscripts = subscripts)
>              panel.text(x = x, y = y + 0.001, lab = lab,
>                         cex = 0.5)
>          })
>
> Deepayan
> --
> http://www.stat.wisc.edu/~deepayan/
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>   


-- 

Eric Archer, Ph.D.
NOAA-SWFSC
8604 La Jolla Shores Dr.
La Jolla, CA 92037
858-546-7121,7003(FAX)
eric.archer at noaa.gov


"Lighthouses are more helpful than churches."
    - Benjamin Franklin

"Cogita tute" - Think for yourself




More information about the R-help mailing list