R语言 ggplot 2中y标度标签的良好K/M/G缩写

iq3niunx  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(78)

提问

我们怎么能 * 容易地 * 有千/兆/千兆标签没有“位”或“千”单位符号?

示例

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"))

字符串
x1c 0d1x的数据
对于y刻度,我期望标签0K5K10K15K20K。没有Kb

附赠问题

是否有任何可用的解决方案来获得01K1M1G标签?即,最合适的缩写值?

bf1o4zei

bf1o4zei1#

试试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 = ""))


taor4pac

taor4pac2#

gdata::humanReadable不适用于负数,它为1000以下的数字添加后缀B,并为所有数字的小数点后添加一位数字,如果指定standard="Unix"而不是standard="SI",则使用1024字节的字节:

> 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"

相关问题