ggplot2 和 scales 包:参数 negative_parens = TRUE 不起作用
ggplot2 and scales package: the parameter negative_parens = TRUE does not work
我试图使用 scales 包为复杂的 table 生成格式。下面的助手应用了 scales 包中的 label_percent 和 label_number_si 函数。
出于某种原因,negative_parens = TRUE 没有产生正确的输出:
prettify_numbers_as_percent <- function(x){
lapply(as.list(as.numeric(x)),label_percent(accuracy = 1, suffix = "%", negative_parens = TRUE, sep = " ")) %>%
unlist() %>%
return() }
prettify_numbers_as_si <- function(x){
lapply(as.list(as.numeric(x)), label_number_si(accuracy = 1, negative_parens = TRUE, sep = " ")) %>%
unlist() %>%
return()
}
当我运行
prettify_numbers_as_si(50000)
prettify_numbers_as_percent(0.05)
我得到了预期的输出:
"50K"
"5%"
当我运行
prettify_numbers_as_si(-50000)
prettify_numbers_as_percent(-0.05)
尽管设置了 negative_parens = TRUE,但我得到了不正确的输出:
"-50K"
"-5%"
有谁知道参数设置失败的原因吗?
问题在于,虽然 scales
包中的其他函数将 negative_parens=
作为参数,但 label_percent
和 label_number_si
却没有。因此,您必须在函数中写入该逻辑:
new_percent <- function(x){
perc <- lapply(
as.list(as.numeric(x)),
label_percent(
accuracy=1, suffix = '%', sep=' '
)
) %>%
unlist()
for(i in 1:length(perc)){
if(substring(perc[i],1,1)=='-'){
perc[i] <- paste0('(',substring(perc[i],2),')')
}
}
return(perc)
}
new_numbers <- function(x){
nums <- lapply(
as.list(as.numeric(x)),
label_number_si(
accuracy = 1, sep = " "
)
) %>%
unlist()
for(i in 1:length(nums)){
if (substring(nums[i],1,1)=='-'){
nums[i] <- paste0('(',substring(nums[i],2),')')
}
}
return(nums)
}
因为您知道 return 中需要放在括号中的每个值都将以 "-"
开头,我使用 for
循环和 substring()
遍历每个项目并将以 "-"
开头的项目转换为以括号开头和结尾。效果很好:
test_perc <- c(-0.5, -0.05, 1.2, .23, -0.13)
test_nums <- c(6000,-60000, 74000, -56000000)
> new_percent(test_perc)
[1] "(50%)" "(5%)" "120%" "23%" "(13%)"
> new_numbers(test_nums)
[1] "6K" "(60K)" "74K" "(56M)"
我试图使用 scales 包为复杂的 table 生成格式。下面的助手应用了 scales 包中的 label_percent 和 label_number_si 函数。
出于某种原因,negative_parens = TRUE 没有产生正确的输出:
prettify_numbers_as_percent <- function(x){
lapply(as.list(as.numeric(x)),label_percent(accuracy = 1, suffix = "%", negative_parens = TRUE, sep = " ")) %>%
unlist() %>%
return() }
prettify_numbers_as_si <- function(x){
lapply(as.list(as.numeric(x)), label_number_si(accuracy = 1, negative_parens = TRUE, sep = " ")) %>%
unlist() %>%
return()
}
当我运行
prettify_numbers_as_si(50000)
prettify_numbers_as_percent(0.05)
我得到了预期的输出:
"50K"
"5%"
当我运行
prettify_numbers_as_si(-50000)
prettify_numbers_as_percent(-0.05)
尽管设置了 negative_parens = TRUE,但我得到了不正确的输出:
"-50K"
"-5%"
有谁知道参数设置失败的原因吗?
问题在于,虽然 scales
包中的其他函数将 negative_parens=
作为参数,但 label_percent
和 label_number_si
却没有。因此,您必须在函数中写入该逻辑:
new_percent <- function(x){
perc <- lapply(
as.list(as.numeric(x)),
label_percent(
accuracy=1, suffix = '%', sep=' '
)
) %>%
unlist()
for(i in 1:length(perc)){
if(substring(perc[i],1,1)=='-'){
perc[i] <- paste0('(',substring(perc[i],2),')')
}
}
return(perc)
}
new_numbers <- function(x){
nums <- lapply(
as.list(as.numeric(x)),
label_number_si(
accuracy = 1, sep = " "
)
) %>%
unlist()
for(i in 1:length(nums)){
if (substring(nums[i],1,1)=='-'){
nums[i] <- paste0('(',substring(nums[i],2),')')
}
}
return(nums)
}
因为您知道 return 中需要放在括号中的每个值都将以 "-"
开头,我使用 for
循环和 substring()
遍历每个项目并将以 "-"
开头的项目转换为以括号开头和结尾。效果很好:
test_perc <- c(-0.5, -0.05, 1.2, .23, -0.13)
test_nums <- c(6000,-60000, 74000, -56000000)
> new_percent(test_perc)
[1] "(50%)" "(5%)" "120%" "23%" "(13%)"
> new_numbers(test_nums)
[1] "6K" "(60K)" "74K" "(56M)"