[R] how to include bar values in a barplot?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 8 10:35:42 CEST 2007

Please see


Note that text() is said to expect a character vector, so why did you 
supply a numeric vector?

   labels: a character vector or expression specifying the _text_ to be
           written.  An attempt is made to coerce other language objects
           (names and calls) to expressions, and vectors and other
           classed objects to character vectors by 'as.character'. If
           'labels' is longer than 'x' and 'y', the coordinates are
           recycled to the length of 'labels'.

and try as.character(vals) for yourself.

> Is there any way to round up those numbers?

See library(fortunes); fortune("Yoda")

On Wed, 8 Aug 2007, Donatas G. wrote:

> On Wednesday 08 August 2007 00:40:56 Donatas G. wrote:
>> On Tuesday 07 August 2007 22:09:52 Donatas G. wrote:
>>> How do I include bar values in a barplot (or other R graphics, where this
>>> could be applicable)?
>>> To make sure I am clear I am attaching a barplot created with
>>> OpenOffice.org which has barplot values written on top of each barplot.
>> After more than two hours search I finally found a solution:
>> http://tolstoy.newcastle.edu.au/R/help/06/05/27286.html
> Hey, the solution happens to be only partiall... If the values are not real
> numbers, and have a lot of digits after the dot, the graph might become
> unreadable...
> see this
> vals <-
> c(1,1.1236886,4.77554676,5.3345245,1,1.1236886,4.77554676,5.3345245,5.5345245,5.4345245,1.1236886,4.77554676,5.3345245,1.1236886,4.77554676,5.3345245)
> names(vals) <- LETTERS[1:16]
> mp <- barplot(vals, ylim = c(0, 6))
> text(mp, vals, labels = vals, pos = 3)
> Is there any way to round up those numbers?
> I tried using
> options(digits=2)
> , and it does change the display of a table, but it does not influence the
> barplot...

Well, it does not affect as.character, nor should it.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list