- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我经常在 GNU R/ggplot 中绘制一些与字节相关的测量图表。内置轴标签可以是普通数字或科学记数法,即 1 MB = 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;
我想知道这个解决方案是否可以改进,因为我的解决方案在每个图中都需要大量样板代码。
最佳答案
我使用了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,...)
}
遵循(例如)scales::comma_format
的格式,但在本例中这似乎是不必要的——只是我不知道的更深层的ggplot2
魔法的一部分完全理解。
OP的代码给出了在我看来不太正确的答案:最右边的轴刻度是“1000K”而不是“1M”——这可以通过更改>1e6
来解决测试>=1e6
。另一方面,f2si
使用小写的k
——我不知道是否需要K
(将结果包装在 >toupper()
可以解决这个问题)。
OP结果(si_vec
):
我的结果(f2si
):
关于r - ggplot2 轴标签中的 SI 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973644/
我正在尝试运行以下命令: -i "2020_11_30_03_37_37.mp4" -i "2020_11_30_03_37_44.jpg" -f lavfi -t 6.016 -i anul
我正在设计一个散点图。在以下示例中,比例函数的输入域为[0.04, 0.9]。 .tickFormat(d3.format(".1s")) 用于轴上,以便在需要时显示 SI 缩写。运行以下截图时,您会
我正在尝试计算下面给定的 c 程序的输出,但无法弄清楚。 为什么 si = 0? #include int main(void){ float p = 1000; float r =
我有一个使用 spring-integration 的代码库。 myCustomFilter 的 bean 工作正常,我在 myDataGetter
是否有 R 函数(或任何包)允许使用标准单位前缀(Kilo、Mega 等...)格式化数字(整数),所以 10 -> 10 1000 -> 1K 0.01 - > 10m 等等... 我可以自己做,但
我正在寻找一个正则表达式来检查 http 表单中的有效 SI 单位输入。例如, kg/m^3 对密度或 m/s^2 为加速。 似乎是在一些开放图书馆中可能已经解决的那种问题;或者可能有一种聪明的方法可
使用文件适配器移动文件你好我想在文件被消耗或失败后移动成功文件夹中的文件出现问题时的文件夹。 我发现了不同的例子,但我想不出一个简单的例子使用文件适配器移动文件的解决方案 可能我在使用 Spring
以我为例: d3.format("~s")(656628); //656.628k 但我的预期结果:656.63k 我想使用 d3-format 选项而不是手动处理它。 现场测试:https://np
我有一个 CSV 文件,其中包含一列,其值以 SI 比例给出因素。我需要对该列进行数字排序。具体来说,CSV 文件包含著名天文物体(Messier 物体)的列表,我需要按距离对它们进行排序。问题是距离
我正在绘制一个图表,该图表获取美元值(value)(从 0 到数百万),并且我试图显示漂亮的刻度。我已经使用 d3.nice 获得了 5 个刻度,所有刻度都有很好的值,这非常酷。但由于差异如此之大,我
我正在寻找一种优雅的方式来使用最合适的前缀来漂亮地打印物理量(例如 12300 克 是 12.3 千克)。一个简单的方法如下所示: def pprint_units(v, unit_str, num_
我正在理论上从高中学习ASM 8086。 当我这样做时:MOV BX,[SI] SI + 1是去BH还是BL? 谢谢 :-) 最佳答案 因为x86是little endian,所以存储位置SI + 1
为了在SI图像中进行漂移校正,如下图所示: 我写代码: number max_shift=5 image src := GetFrontImage() number sx, sy, sz src.Ge
我经常在 GNU R/ggplot 中绘制一些与字节相关的测量图表。内置轴标签可以是普通数字或科学记数法,即 1 MB = 1e6。我想要 SI 前缀(Kilo = 1e3、Mega=1e6、Giga
我不明白有什么区别。 最佳答案 当您使用 movsb 等指令时,si 被视为源源寄存器,而 di 被视为目标目标寄存器。但它们都是普通的 x86 寄存器。 关于assembly - SI 和 DI 寄
我准备了一个小型 WPF 应用程序: 查看: View-Model 如下所示: 现在,出于某种原因,斯洛文尼亚文化的日、月和年之间有空格: 我的意思是,我得到的不是 14.01.2019,而是 14.
在高网络流量下,在CentOS上使用LVS或Nginx进行负载均衡。网络流量主要是具有大量连接和相对低带宽的小数据包。 唯一似乎成为瓶颈的是高 SoftIRQ,这对基于事件的 IO 来说并不奇怪。 机
嗯,这显然是一个初学者的问题,但这是我第一次尝试用 C 制作操作系统(实际上,我对 C 几乎完全陌生.. 我已经习惯了 asm) 那么,为什么这到底是无效的呢?据我所知,C 中的pointer 只是一
我正在寻找一个支持在各种 SI 前缀之间转换数字的 python 库,例如,kilo 到 pico,nano 到 giga 等等。你会推荐什么? 最佳答案 我移植了一个简单的函数(original C
我想在医学环境中的 Modelica 模型中使用非 SI 单位。我在 Dymola 中使用 Modelica。作为一名工程师,我更喜欢 SI 单位。但是我的模型参数来自医学出版物,我想避免转换错误。医
我是一名优秀的程序员,十分优秀!