[R] Display values in piechart/barplot

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Sep 29 19:01:54 CEST 2005

On Thu, 2005-09-29 at 14:34 +0200, Volker Rehbock wrote:
> Is it possible to automatically display the underlying values of a 
> piechart/barplot in the graphic? If so, which package/function/argument do I 
> need for it?
> Thanks,
> Volker

Using pie charts are not a particularly good way of displaying data,
even though the pie() function is available in R. See ?pie for more
information on this.

For barplots, the following provide two approaches:

# Place the bar values above the bars
# Note that I set 'ylim' to make room for 
# the text labels above the bars

vals <- 1:5
names(vals) <- LETTERS[1:5]
mp <- barplot(vals, ylim = c(0, 6))
text(mp, vals, labels = vals, pos = 3)

# Place the bar values below the x axis

vals <- 1:5
names(vals) <- LETTERS[1:5]
mp <- barplot(vals)
mtext(side = 1, at = mp, text = vals, line = 3)

Note that barplot() returns the bar midpoints, so you can use these
values for annotation placement.

See ?barplot, ?text and ?mtext for more information.


Marc Schwartz

More information about the R-help mailing list