ggplot()使用scale :: percent_format()缩放产生奇怪的结果 [英] ggplot() scaling with scale::percent_format() producing strange results
问题描述
library(tidyverse)
mtcars %>%
count(cyl) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(x = cyl, y = prop)) +
geom_point() +
scale_y_continuous(labels = scales::percent_format(accuracy = 5L))
如果我使用上面的scales::percent()
而不是scales::percent_format(accuracy = 5L)
,我会在百分比标签中获得小数位,这是我不想要的.
问题-在上述示例中, 5L的作用是什么?为什么需要使用整数5L而不是5?为什么6L将最高y值从40%更改为42%?那真是奇怪.
首先,不需要将其精确地指定为整数(即5
可以正常工作 ).>
第二,您可以随时在R控制台中进行?scales::percent_format
(免费!).这样做可以告诉您有关该功能的信息:
percent_format(
accuracy = NULL, scale = 100, prefix = "", suffix = "%",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...
)
因此,它需要许多可能的参数,所有参数都有默认值,有些是选项(通过...
).
accuracy
参数的默认值为NULL
.如果我们仅在功能的帮助页面上向下滚动,就会看到:
-
accuracy
:四舍五入到的数字,NULL
用于自动猜测.
如果键入不带括号或?
前缀的函数名称,则可以看到整个源代码.这样做表明它最终会调用scales::number()
,其定义为:
function (x, accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...) {
if (length(x) == 0) return(character())
accuracy <- accuracy %||% precision(x)
x <- round_any(x, accuracy/scale)
nsmall <- -floor(log10(accuracy))
nsmall <- min(max(nsmall, 0), 20)
ret <- format(scale * x, big.mark = big.mark, decimal.mark = decimal.mark,
trim = trim, nsmall = nsmall, scientific = FALSE, ...)
ret <- paste0(prefix, ret, suffix)
ret[is.infinite(x)] <- as.character(x[is.infinite(x)])
ret[is.na(x)] <- NA
ret
}
此:
accuracy <- accuracy %||% precision(x)
说明accuracy
是否不是NULL
,请使用它,否则使用precision()
函数进行猜测.
此后的下一行是您问题的最终答案.
library(tidyverse)
mtcars %>%
count(cyl) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(x = cyl, y = prop)) +
geom_point() +
scale_y_continuous(labels = scales::percent_format(accuracy = 5L))
If I use scales::percent()
above instead of scales::percent_format(accuracy = 5L)
I get decimal places in my percentage labels, which I don't want.
The question - what does 5L do in my example above? Why do I need to use the integer 5L instead of 5? And why does 6L change the highest y-value from 40% to 42%? That's just plain strange.
First, it doesn't need to be precisely specified as an integer (i.e. 5
works just fine).
Second, you can do ?scales::percent_format
at any time in an R console (it's free!). Doing so tells you this about the function:
percent_format(
accuracy = NULL, scale = 100, prefix = "", suffix = "%",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...
)
So, it takes many possible parameters all of which have defaults and some are options (via ...
).
The default for the accuracy
parameter is NULL
. If we scroll down just a bit on the help page for the function we see:
accuracy
: Number to round to,NULL
for automatic guess.
If we type the function name without parens or a ?
prefix, we can see the entire source. Doing so shows that it ultimately calls scales::number()
which is defined as:
function (x, accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...) {
if (length(x) == 0) return(character())
accuracy <- accuracy %||% precision(x)
x <- round_any(x, accuracy/scale)
nsmall <- -floor(log10(accuracy))
nsmall <- min(max(nsmall, 0), 20)
ret <- format(scale * x, big.mark = big.mark, decimal.mark = decimal.mark,
trim = trim, nsmall = nsmall, scientific = FALSE, ...)
ret <- paste0(prefix, ret, suffix)
ret[is.infinite(x)] <- as.character(x[is.infinite(x)])
ret[is.na(x)] <- NA
ret
}
This:
accuracy <- accuracy %||% precision(x)
says if accuracy
is not NULL
use it otherwise guess by using the precision()
function.
The next line after that is the ultimate answer to your question.
这篇关于ggplot()使用scale :: percent_format()缩放产生奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!