r - ggplot2 中 y 刻度标签的良好 K/M/G 缩写

标签 r ggplot2 format axis-labels

问题

我们如何轻松拥有没有“位”或“字节”单位符号的千/兆/千兆标签?

例子

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 scale with Kb instead of K

对于 y 尺度,我希望标签为 0K5K10K15K 20K。没有Kb!

奖金问题

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

最佳答案

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

enter image description here


编辑:

我们可以自定义函数:

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

enter image description here

关于r - ggplot2 中 y 刻度标签的良好 K/M/G 缩写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59172620/

相关文章:

r - 列表中的总项目数

r - 选择某些(未知)索引之间的行

r - 自定义李克特图中条形图的宽度

c++ - 从 Windows 编辑控件的格式中删除逗号的方法? (C++)

xml - XSLT 1.0 : How can I format a paragraph over multiple lines whilst keeping a token intact?

r - 在 R 中输出多个绘图(用于动画)

r - 在 ubuntu 界面上的 R 中安装 e1071

减少 grid.arrange 图之间的空间

r - 如何将 x 标签移动到 R 中 ggplot 中的分面标签上

delphi - 在 Delphi 中设置 DBGrid 列格式