[R] Data values on graphics
Robert Baer
rbaer at atsu.edu
Tue May 24 15:41:10 CEST 2011
>> zcatav wrote:
>>>
>>> How can i add data values or proportion values on bar or pie charts?
>>>
>> You can add any text or numbers to any place of a graph with this
>> command:
>>> text(45,20,"some text")
>> where: 45 is position on the X-axis and 20 - on the Y. Note that numbers
>> denote the MIDDLE of the text,not its bottom-left corner. Must work
>> perfectly for barplots, although not sure about pie-charts.
>>
>
> It's o.k. to add text,but i mean data values representing colon.
> Let's we have following table;
> A B
> 1 2 3
> 2 4 5
> 3 3 6
>
> I want to see these values (or % values) on representing bar. And if
> possible R makes it automatically.
> Thanks.
Maybe this example will help for barplots.
df = structure(list(A = c(2L, 4L, 3L), B = c(3L, 5L, 6L)),
.Names = c("A", "B"), class = "data.frame",
row.names = c("1", "2", "3"))
vec = c(mean(df$A), mean(df$B)) #Bars assumed to be column means
# bp stores the x locations for text
bp = barplot(vec)
# At top of bars
text(bp, vec, vec, xpd = T) # xpd = T prevents clipping
# Alternative with txt placement cleaner output
text(bp, c(2, 2), round(vec,1))
# Doing percents
pc = 100*vec/sum(vec)
text(bp, c(1, 1), paste(round(pc,1), '%', sep=' '))
------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965
More information about the R-help
mailing list