16

我经常在 GNU R / ggplot 中为一些与字节相关的测量绘制图表。内置轴标签是纯数字或科学记数法,即 1 兆字节 = 1e6。我想要 SI 前缀(Kilo = 1e3、Mega=1e6、Giga=1e9 等),即轴应标记为 1.5K、5K、1M、150M、4G 等。

我目前使用以下代码:

si_num <- function (x) {

  if (!is.na(x)) {
    if (x > 1e6) { 
      chrs <- strsplit(format(x, scientific=12), split="")[[1]];
      rem <- chrs[seq(1,length(chrs)-6)];
      rem <- append(rem, "M");
    }

    else if (x > 1e3) { 
      chrs <- strsplit(format(x, scientific=12), split="")[[1]];
      rem <- chrs[seq(1,length(chrs)-3)];
      rem <- append(rem, "K");
    }
    else {
      return(x);
    }

    return(paste(rem, sep="", collapse=""));
  }
  else return(NA);
} 

si_vec <- function(x) {
  sapply(x, FUN=si_num);
}

library("ggplot2");

bytes=2^seq(0,20) + rnorm(21, 4, 2);
time=bytes/(1e4 + rnorm(21, 100, 3)) + 8;

my_data = data.frame(time, bytes);

p <- ggplot(data=my_data, aes(x=bytes, y=time)) +
     geom_point() +
     geom_line() +
     scale_x_log10("Message Size [Byte]", labels=si_vec) +
     scale_y_continuous("Round-Trip-Time [us]");
p;

我想知道是否可以改进此解决方案,因为我的解决方案在每个图表中都需要大量样板代码。

4

2 回答 2

29

library("sos"); findFn("{SI prefix}")以前是找sitools包的。

构造数据:

bytes <- 2^seq(0,20) + rnorm(21, 4, 2)
time <- bytes/(1e4 + rnorm(21, 100, 3)) + 8
my_data <- data.frame(time, bytes)

加载包:

library("sitools")
library("ggplot2")    

创建情节:

(p <- ggplot(data=my_data, aes(x=bytes, y=time)) +
     geom_point() +
     geom_line() +
     scale_x_log10("Message Size [Byte]", labels=f2si) +
     scale_y_continuous("Round-Trip-Time [us]"))

我不确定这与您的功能相比如何,但至少有人费心编写它......

我稍微修改了您的代码样式-行尾的分号是无害的,但通常是 MATLAB 或 C 编码器的符号...

编辑:我最初定义了一个通用格式化函数

si_format <- function(...) {
    function(x) f2si(x,...)
}

遵循 (eg) 的格式scales::comma_format,但在这种情况下似乎没有必要——这只是ggplot2我不完全理解的更深层魔法的一部分。

OP 的代码给出了在我看来并不完全正确的答案:最右边的轴刻度是“1000K”而不是“1M”——这可以通过将>1e6测试更改为>=1e6. 另一方面,f2si使用小写k——我不知道是否K需要(将结果包装起来toupper()可以解决这个问题)。

手术结果(si_vec):

在此处输入图像描述

我的结果(f2si):

在此处输入图像描述

于 2012-12-20T14:51:48.863 回答
5

更新:该软件包的最新版本scales包括打印可读标签的功能。

在这种情况下,label_bytes可以使用:

library(ggplot2)
library(scales)

bytes <- 2^seq(0,20) + rnorm(21, 4, 2)

my_data <- data.frame(
    bytes=as.integer(bytes),
    time=bytes / (1e4 + rnorm(21, 100, 3)) + 8
)

ggplot(data=my_data, aes(x=bytes, y=time)) +
    geom_point() +
    geom_line() +
    scale_x_log10("Message Size [Byte]", labels=label_bytes()) +
    scale_y_continuous("Round-Trip-Time [us]")

scales-si-标签

或者,如果您更喜欢使用 IEC 单位(KiB = 2^10MiB = 2 ^ 20、 ...),请指定labels=label_bytes(units = "auto_binary")。对于结果,请查看下面原始答案中的第二个图,因为结果非常相似。


原始答案

对于字节有gdata::humanReadable. humanReadable支持 SI 前缀(1000 字节 = 1 KB)以及IEC 定义的二进制前缀(1024 字节 = 1 KiB)。

此函数humanReadableLabs允许自定义参数并处理NA值:

humanReadableLabs <- function(...) {
    function(x) {
        sapply(x, function(val) {
            if (is.na(val)) {
                return("")
            } else {
                return(
                    humanReadable(val, ...)
                )
            }
        })
    }
}

现在可以直接更改标签以使用 SI 前缀和“字节”作为单位:

library(ggplot2)
library(gdata)

bytes <- 2^seq(0,20) + rnorm(21, 4, 2)

my_data <- data.frame(
    bytes=as.integer(bytes),
    time=bytes / (1e4 + rnorm(21, 100, 3)) + 8
)

humanReadableLabs <- function(...) {...}

ggplot(data=my_data, aes(x=bytes, y=time)) +
    geom_point() +
    geom_line() +
    scale_x_log10("Message Size [Byte]", labels=humanReadableLabs(standard="SI")) +
    scale_y_continuous("Round-Trip-Time [us]")

si标签

通过省略 绘制 IEC 前缀standard="SI"。请注意,还必须指定中断以具有清晰易读的值。

ggplot(data=my_data, aes(x=bytes, y=time)) +
    geom_point() +
    geom_line() +
    scale_x_log10("Message Size [Byte]", labels=humanReadableLabs()) +
    scale_y_continuous("Round-Trip-Time [us]")

iec标签

于 2019-12-16T19:28:09.800 回答