On Apr 24, 2010, at 5:44 AM, Sebastian Bergmann wrote:

> Hello!
> 
> I started using R today to produce a bar chart that I can use in LaTeX.
> 
> Here is what I have got so far:
> 
>   pdf("bugfix_costs.pdf")
>   costs <- c(1, 5, 10, 20, 50, 150)
>   barplot(costs,
>           ylab="Relative cost of a bugfix",
>           names.arg=c("Requirements", "Design", "Code",
>           "Developer Tests", "Acceptance Tests", "Operations"),
>           cex.names=0.6
>          )
>   dev.off()
>   q()
> 
> What I am missing from the produced chart is "highlighted values" as in
> http://www.slideshare.net/spriebsch/die-5-goldenen-oopregeln-fr-php, for
> instance. Basically what I need is to put text above each of the bars.
> Is that possible?
> 
> Thanks!


You will find that most folks here will suggest that you not put the values on 
top of the bars, as they alter the visual perception of the graphic. A better 
approach is to put them below each bar on the x axis. Others will suggest that 
you use a ?dotchart instead of a barplot.

A key feature to note is that barplot() returns the x axis values of the bar 
midpoints, which enables the alignment of annotations:

costs <- c(1, 5, 10, 20, 50, 150)

mp <- barplot(costs, ylab = "Relative cost of a bugfix", 
              names.arg = c("Requirements", "Design", "Code", "Developer 
Tests", 
                            "Acceptance Tests", "Operations"),
                          cex.names = 0.6)

mtext(1, at = mp, text = costs, cex = 0.6, line = 2)


See ?mtext for more information


If you really want the values on top of the bars, use text() instead of 
mtext(). You will also need to increase the max value of the y axis to make 
room for the labels using the ylim argument in barplot():


costs <- c(1, 5, 10, 20, 50, 150)

mp <- barplot(costs, ylab = "Relative cost of a bugfix", 
              names.arg = c("Requirements", "Design", "Code", "Developer 
Tests", 
                            "Acceptance Tests", "Operations"),
                          cex.names=0.6, ylim = c(0, max(costs) * 1.2))

text(mp, costs, labels = costs, pos = 3)


See ?text for more information.

HTH,

Marc Schwartz

______________________________________________
R-help@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.

Reply via email to