Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format numbers in charts as percentages [duplicate]

Tags:

r

Possible Duplicate:
How to format a number as percentage in R?

This must be a newbie questions, but how do I format numbers in charts (say PerformanceSummary in PerformanceAnalytics) as percentages? The data I use are in the format of 0.04 etc. Everything is working fine, except I want the charts to show percentages.

Thanks in advance

EDIT:

Here is how I am doing it now (with standard data for simplicity reasons):

library("PerformanceAnalytics")
data(managers)
charts.PerformanceSummary(managers)

I put it here while I go googling for providing my own labels.

like image 754
Josef Magnusson Avatar asked Oct 21 '11 10:10

Josef Magnusson


People also ask

How do you convert a number to a percent in a bar chart?

Select the decimal number cells, and then click Home > % to change the decimal numbers to percentage format. 7. Then go to the stacked column, and select the label you want to show as percentage, then type = in the formula bar and select percentage cell, and press Enter key.


1 Answers

Unless I miss something, I don't think this is a newbie question at all.

(Disclaimer: I think this answer isn't particularly elegant, and the graphic sucks. But hopefully it can serve as a template so somebody else can improve and post some proper code.)

Here is one way of doing it in base graphics:

  • Set up the plot without the y-axis, i.e. use yaxt="n"
  • Calculate where you want the labels to be located, e.g. using seq
  • use sprintf and round to format the labels as text with percentage sign

Some code:

set.seed(1)
x <- runif(10)
plot(x, type="h", yaxt="n")
yLabels <- seq(0.2, 0.8, 0.2)
axis(2, at=yLabels, labels=sprintf(round(100*yLabels), fmt="%2.2f%%"), las=1)

The plot:

enter image description here

like image 187
Andrie Avatar answered Oct 10 '22 14:10

Andrie