将cut()生成的标签格式化为百分比

krl*_*mlr 17 r

我需要cut在连续变量上应用ggplot2中的Brewer颜色标度来显示它,就像在ggplot2中使用scale_fill_brewer()函数设置数据的断点一样.连续变量是相对差异,我想将数据格式化为"18.2%"而不是"0.182".有没有一种简单的方法来实现这一目标?

x <- runif(100)
levels(cut(x, breaks=10))

[1] "(0.0223,0.12]" "(0.12,0.218]"  "(0.218,0.315]" "(0.315,0.413]"
[5] "(0.413,0.511]" "(0.511,0.608]" "(0.608,0.706]" "(0.706,0.804]"
[9] "(0.804,0.901]" "(0.901,0.999]"
Run Code Online (Sandbox Code Playgroud)

我想,例如,第一级出现为(2.23 %, 12 %].有更好的选择cut吗?

krl*_*mlr 17

我已经cut_format()在我的kimisc软件包的0.2-3版本中实现了,现在版本0.3在CRAN上.

# devtools::install_github("krlmlr/kimisc")
x <- seq(0.1, 0.9, by = 0.2)

breaks <- seq(0, 1, by = 0.25)

cut(x, breaks)
## [1] (0,0.25]   (0.25,0.5] (0.25,0.5] (0.5,0.75] (0.75,1]  
## Levels: (0,0.25] (0.25,0.5] (0.5,0.75] (0.75,1]

cut_format(x, breaks, format_fun = scales::percent)
## [1] (0%, 25%]   (25%, 50%]  (25%, 50%]  (50%, 75%]  (75%, 100%]
## Levels: (0%, 25%] (25%, 50%] (50%, 75%] (75%, 100%]
Run Code Online (Sandbox Code Playgroud)

它仍然不完美,传递休息次数(如原始示例中所示)尚不起作用.


Jam*_*mes 10

gsub将原始数据乘以100后,使用一些正则表达式:

gsub("([0-9.]+)","\\1%",levels(cut(x*100,breaks=10)))
 [1] "(0.449%,10.4%]" "(10.4%,20.3%]"  "(20.3%,30.2%]"  "(30.2%,40.2%]"  "(40.2%,50.1%]"  "(50.1%,60%]"    "(60%,69.9%]"    "(69.9%,79.9%]"  "(79.9%,89.8%]"  "(89.8%,99.7%]"
Run Code Online (Sandbox Code Playgroud)


A5C*_*2T1 6

为什么不复制代码cut.default并使用修改后的级别创建自己的版本?看到这个要点.

改变了两条线:

第22行:ch.br <- formatC(breaks, digits = dig, width = 1)改为ch.br <- formatC(breaks*100, digits = dig, width = 1).

第29行:else "[", ch.br[-nb], ",", ch.br[-1L], if (right)改为else "[", ch.br[-nb], "%, ", ch.br[-1L], "%", if (right)

其余的都是一样的.在这里,它正在行动:

library(devtools)
source_gist(4593967)

set.seed(1)
x <- runif(100)
levels(cut2(x, breaks=10))
#  [1] "(1.24%, 11%]"   "(11%, 20.9%]"   "(20.9%, 30.7%]" "(30.7%, 40.5%]" "(40.5%, 50.3%]"
#  [6] "(50.3%, 60.1%]" "(60.1%, 69.9%]" "(69.9%, 79.7%]" "(79.7%, 89.5%]" "(89.5%, 99.3%]"
Run Code Online (Sandbox Code Playgroud)


krl*_*mlr 3

新的{santoku} 包现在提供了一种在开发版本中执行此操作的方法:

library(santoku)

set.seed(20200607)
x <- runif(20)

chop_evenly(x, 10, labels = lbl_intervals(fmt = percent))
#>  [1] [33.13%, 42.11%) [60.08%, 69.06%) [69.06%, 78.04%) [69.06%, 78.04%)
#>  [5] [87.02%, 96%]    [6.193%, 15.17%) [15.17%, 24.15%) [6.193%, 15.17%)
#>  [9] [33.13%, 42.11%) [6.193%, 15.17%) [87.02%, 96%]    [51.1%, 60.08%) 
#> [13] [42.11%, 51.1%)  [6.193%, 15.17%) [42.11%, 51.1%)  [6.193%, 15.17%)
#> [17] [6.193%, 15.17%) [69.06%, 78.04%) [78.04%, 87.02%) [87.02%, 96%]   
#> 9 Levels: [6.193%, 15.17%) [15.17%, 24.15%) ... [87.02%, 96%]
tab_evenly(x, 10, labels = lbl_intervals(fmt = scales::label_percent(accuracy = 0.1)))
#> x
#>  [6.2%, 15.2%) [15.2%, 24.2%) [33.1%, 42.1%) [42.1%, 51.1%) [51.1%, 60.1%) 
#>              6              1              2              2              1 
#> [60.1%, 69.1%) [69.1%, 78.0%) [78.0%, 87.0%) [87.0%, 96.0%] 
#>              1              3              1              3
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-06-09 创建