-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot2_formatter.R
71 lines (61 loc) · 2.47 KB
/
ggplot2_formatter.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## CREATED BY fdryan
## https://github.com/fdryan/R
## I take no credit for any of the work in this script.
require(plyr)
require(scales)
# ---------------------------------------------------------------------------------------------
# Formatting functions for ggplot graph axis
# ---------------------------------------------------------------------------------------------
#' Human Numbers: Format numbers so they're legible for humans
#' Use this in ggplot for labels where you might use the comma or percent functions from the
#' Scales package.
#'
#' Checks whether numbers are positive or negative.
#' Allows up to 1 significant figure
#' sapply used for element-wise application of the humanity function as a vector may include
#' numbers where billions, millions or thousands are appropriate.
#'
#' @return a character vector the same length as the input vector
#' @param x a numeric vector to format,
#' @smbl a symbol you'd like to prefix your numbers by e.g. "$"
#' @signif = the number of significant places you want the function to return
#' @examples
#' human_numbers(c(1000000 , 1500000, 10000000000))
#' human_numbers(c(1.200000e+05, -2.154660e+05, 2.387790e+05, 4.343500e+04 ,5.648675e+12), "$")
#' ggplot2 + scale_y_continuous(labels = human_numbers)
#' ggplot2 + scale_x_continuous(labels = human_numbers)
#' ggplot2 + scale_x_continuous(labels = human_gbp)
human_numbers <- function(x = NULL, smbl ="", signif = 1){
humanity <- function(y){
if (!is.na(y)){
tn <- round(abs(y) / 1e12, signif)
b <- round(abs(y) / 1e9, signif)
m <- round(abs(y) / 1e6, signif)
k <- round(abs(y) / 1e3, signif)
if ( y >= 0 ){
y_is_positive <- ""
} else {
y_is_positive <- "-"
}
if ( k < 1 ) {
paste0( y_is_positive, smbl, round(abs(y), signif ))
} else if ( m < 1){
paste0 (y_is_positive, smbl, k , "k")
} else if (b < 1){
paste0 (y_is_positive, smbl, m ,"m")
}else if(tn < 1){
paste0 (y_is_positive, smbl, b ,"bn")
} else {
paste0 (y_is_positive, smbl, comma(tn), "tn")
}
} else if (is.na(y) | is.null(y)){
"-"
}
}
sapply(x,humanity)
}
#' Human versions of large currency numbers - extensible via smbl
human_gbp <- function(x){human_numbers(x, smbl = "£")}
human_usd <- function(x){human_numbers(x, smbl = "$")}
human_euro <- function(x){human_numbers(x, smbl = "€")}
human_num <- function(x){human_numbers(x, smbl = "")}