我们怎样才能轻松地拥有没有“位”或“字节”单位符号的千/兆/千兆标签?
data.frame(x = LETTERS[1:5], n = c(0, 5000, 10000, 15000, 20000)) %>%
ggplot(aes(x, n)) +
geom_point() +
scale_y_continuous(labels = scales::number_bytes_format(units = "si"))
对于 y 尺度,我期望标签
0K
、5K
、10K
、15K
、20K
。不Kb
!
是否有任何可用的解决方案来获取
0
、1K
、1M
、1G
标签?即,最合适的值缩写?
尝试
gdata::humanReadable
:
library(ggplot2)
library(gdata)
myDat <- data.frame(x = LETTERS[1:5], n = c(0, 5000, 10000, 15000, 20000))
ggplot(myDat, aes(x, n)) +
geom_point() +
scale_y_continuous(breaks = myDat$n,
labels = humanReadable(myDat$n, standard = "Unix", sep = ""))
编辑:
我们可以定制功能:
humanReadableCustom <- function (x, units = "auto", standard = c("IEC", "SI", "Unix"),
digits = 1, width = NULL, sep = " ", justify = c("right",
"left"))
{
#suffix.SI <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
# custom
suffix.SI <- c("", "K", "M", "G", "T", "P", "E", "Z", "Y")
suffix.IEC <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
suffix.Unix <- c("B", "K", "M", "G", "T", "P", "E", "Z", "Y")
standard <- match.arg(standard)
if (length(justify) == 1)
justify <- c(justify, justify)
.applyHuman <- function(x, base, suffix, digits, width,
sep) {
n <- length(suffix)
i <- pmax(pmin(floor(log(x, base)), n - 1), 0)
if (!is.finite(i))
i <- 0
x <- x/base^i
if (is.null(width))
x <- format(round(x = x, digits = digits), nsmall = digits)
else {
lenX <- nchar(x)
if (lenX > width) {
digits <- pmax(width - nchar(round(x)) - 1,
0)
}
if (i == 0)
digits <- 0
x <- round(x, digits = digits)
}
c(x, suffix[i + 1])
}
if (any(x < 0))
stop("'x' must be positive")
if (standard == "SI") {
suffix <- suffix.SI
base <- 10^3
}
else if (standard == "IEC") {
suffix <- suffix.IEC
base <- 2^10
}
else {
suffix <- suffix.Unix
base <- 2^10
}
if (!missing(units) && units == "bytes") {
retval <- rbind(x, "bytes")
}
else if (!missing(units) && units != "auto") {
units <- suffix[match(toupper(units), toupper(suffix))]
power <- match(units, suffix) - 1
X <- x/(base^power)
X <- format.default(x = X, digits = digits, nsmall = digits)
retval <- rbind(X, rep(units, length(X)))
}
else retval <- sapply(X = x, FUN = ".applyHuman", base = base,
suffix = suffix, digits = digits, width = width, sep = sep)
if (all(justify == "none"))
paste(trim(retval[1, ]), trim(retval[2, ]), sep = sep)
else paste(format(trim(retval[1, ]), justify = justify[1]),
format(trim(retval[2, ]), justify = justify[2]), sep = sep)
}
然后剧情
library(ggplot2)
library(gdata)
myDat <- data.frame(x = LETTERS[1:5], n = c(0, 5000, 10000, 15000, 20000))
ggplot(myDat, aes(x, n)) +
geom_point() +
scale_y_continuous(breaks = myDat$n,
labels = humanReadableCustom(myDat$n,
standard = "SI", sep = ""))
gdata::humanReadable
不适用于负数,它会为 1000 以下的数字添加后缀 B,并且会在所有数字的小数点后添加一位数字,如果您指定 standard="Unix"
而不是,它会使用 1024 字节千字节standard="SI"
:
> gdata::humanReadable(-10,standard="Unix",sep="")
Error in gdata::humanReadable(-10, standard = "Unix", sep = "") :
'x' must be positive
> gdata::humanReadable(2500,standard="Unix",sep="")
[1] "2.4K"
> gdata::humanReadable(2500,standard="SI",sep="")
[1] "2.5kB"
> gdata::humanReadable(10,standard="SI",sep="")
[1] "10.0B"
您可以将
scale_y_continuous(labels=kimi)
与此功能一起使用:
kimi=\(x){
e=floor(log10(abs(x)))
e2=pmax(e,0)%/%3+1
suf=c("","k","M","B","T")
x[]=ifelse(abs(x)<1,x,paste0(round(x/1e3^(e2-1),ifelse(e%%3==0,1,0)),suf[e2]))
x
}
kimi(c(-2.3e9,-12345,-12,0,50,200,2235,12345,126583,125467080))
[1] "-2B" "-12.3k" "-12" "0" "50" "200" "2k" "12.3k"
[9] "127k" "125M"