Skip to content

Instantly share code, notes, and snippets.

@terashim
Last active February 10, 2022 09:31
Show Gist options
  • Save terashim/c427f40b599388eb0afacb9356f6e8f3 to your computer and use it in GitHub Desktop.
Save terashim/c427f40b599388eb0afacb9356f6e8f3 to your computer and use it in GitHub Desktop.
scales::comma() のデフォルト accuracy の挙動確認

accuracy を明示的に指定

scales::comma(c(0L, 1L), accuracy = 1)
## [1] "0" "1"
scales::comma(c(0L, 1L), accuracy = 0.1)
## [1] "0.0" "1.0"
scales::comma(c(0L, 1L), accuracy = 0.01)
## [1] "0.00" "1.00"

accuracy を指定しない

scales::comma(c(0L, 1L))
## [1] "0.0" "1.0"
scales::comma(c(0L, 9L))
## [1] "0.0" "9.0"
scales::comma(c(0L, 10L))
## [1] "0"  "10"

=> 最も近い2つの値の差が9以下の場合、accuracyが0.1以下になる。 特に、元のベクトルが整数型でも小数点が付く。

参考

https://scales.r-lib.org/reference/number_format.html

accuracy Number to round to. Use (e.g.) 0.01 to show 2 decimal places of precision. If NULL, the default, uses a heuristic that should ensure breaks have the minimum number of digits needed to show the difference between adjacent values.

Applied to rescaled data.

#' ---
#' title: "scales::comma() のデフォルト accuracy の挙動確認"
#' author: terashim
#' output:
#' html_document:
#' keep_md: true
#' ---
#' ### accuracy を明示的に指定
scales::comma(c(0L, 1L), accuracy = 1)
scales::comma(c(0L, 1L), accuracy = 0.1)
scales::comma(c(0L, 1L), accuracy = 0.01)
#' ### accuracy を指定しない
scales::comma(c(0L, 1L))
scales::comma(c(0L, 9L))
scales::comma(c(0L, 10L))
#' => 最も近い2つの値の差が9以下の場合、accuracyが0.1以下になる。
#' 特に、元のベクトルが整数型でも小数点が付く。
#' ### 参考
#'
#' https://scales.r-lib.org/reference/number_format.html
#'
#' > accuracy
#' > Number to round to. Use (e.g.) 0.01 to show 2 decimal places of precision. If NULL, the default, uses a heuristic that should ensure breaks have the minimum number of digits needed to show the difference between adjacent values.
#' >
#' > Applied to rescaled data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment