是否有一个选项可以设置“,”作为 ggplot 中轴标签的默认小数点?

发布于 2025-01-13 17:37:22 字数 946 浏览 3 评论 0原文

制作图表时,ggplot2 有许多合理的比例默认值,当尝试使用 scales 获得相同结果时,需要额外的工作。

示例:

library("ggplot2")
ggplot(mtcars, aes(drat, mpg)) + geom_point()

请注意,该图小数点后面有一位数字。

但是,我住在荷兰,我们习惯使用逗号作为小数点。这在 scale_x_continuous 中很容易完成:

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = scales::label_number(big.mark = ".", decimal.mark = ","))

这个解决方案让我感到困扰的是,这也增加了位数:每个标签末尾有一个额外的、相当不必要的 0。当然,这也可以在 scales::label_number() 中通过设置 accuracy = 0.1 来解决,但这需要迭代(绘制和重新绘制以设置更合理的值)位数)。

是否有一个选项可以修复 ggplot 使用的默认小数点?我正在寻找一个解决方案,

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

返回与

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = scales::label_number(
    big.mark = ".", 
    decimal.mark = ",",
    accuracy = 0.1
  ))

When making a graph, ggplot2 has a lot of sensible defaults for scales that require extra work when trying to achieve the same result using scales.

An example:

library("ggplot2")
ggplot(mtcars, aes(drat, mpg)) + geom_point()

Note that this plot has one digit behind the decimal mark.

However, I live in the Netherlands and we're used to using a comma as decimal mark. This is easily done within scale_x_continuous:

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = scales::label_number(big.mark = ".", decimal.mark = ","))

What bugs me about this solution is that this also increases the number of digits: there's an extra, rather unnecessary, 0 at the end of each label. Of course, this can also be solved within scales::label_number(), by setting accuracy = 0.1 but that requires iteration (plotting and re-plotting to set the more sensible number of digits).

Is there an option to fix the default decimal mark used by ggplot? I'm looking for a solution where

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

returns the same plot as

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = scales::label_number(
    big.mark = ".", 
    decimal.mark = ",",
    accuracy = 0.1
  ))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2025-01-20 17:37:22

并非特定于 ggplot2,但您可以使用 options(OutDec = ",") 全局设置输出小数点字符。

从帮助页面:

OutDec:包含单个字符的字符串。首选
在输出转换中用作小数点的字符,即
处于打印、绘图、格式和 as.character 状态,但不在时
deparsing 也不是 sprintf 也不是 formatC (有时在之前使用
打印。)

library(ggplot2)

options(OutDec= ",")

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

在此处输入图像描述

Not specific to ggplot2 but you can set the output decimal point character globally using options(OutDec = ",").

From the help page:

OutDec: character string containing a single character. The preferred
character to be used as the decimal point in output conversions, that
is in printing, plotting, format and as.character but not when
deparsing nor by sprintf nor formatC (which are sometimes used prior
to printing.)

library(ggplot2)

options(OutDec= ",")

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

enter image description here

百变从容 2025-01-20 17:37:22

关键似乎是 format (来自基础 R)和 scales::number 使用不同的规则。我们可以恢复使用 format ...

myf <- function(x, ...) format(x, big.mark = ".", decimal.mark = ",", ...)
ggplot(mtcars, aes(drat, mpg)) +
  geom_point() +
  scale_x_continuous(labels = myf)

如果您想让这些标签成为全局默认标签,我认为您可以这样做:

scale_x_continuous <- function(..., labels = myf) {
  do.call(ggplot2::scale_x_continuous, c(list(...), labels = labels))
}

The key seems to be that format (from base R) and scales::number use different rules. We can revert to using format ...

myf <- function(x, ...) format(x, big.mark = ".", decimal.mark = ",", ...)
ggplot(mtcars, aes(drat, mpg)) +
  geom_point() +
  scale_x_continuous(labels = myf)

If you want to make these labels the global default I think you can do this:

scale_x_continuous <- function(..., labels = myf) {
  do.call(ggplot2::scale_x_continuous, c(list(...), labels = labels))
}
女皇必胜 2025-01-20 17:37:22

更通用的解决方案是编写一个与 {scales} 兼容的标签函数 label_format() 在底层调用 base::format(),然后根据荷兰标点符号惯例制作一个专门的版本。

library(ggplot2)
library(scales)

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

# Use base::format as a {scales}-compatible labels function
label_format <- function(...) {
  function(x) do.call(format, c(list(x = x), list(...)))
}

# Specialized version for Dutch punctuation
label_format_NL <- function(...) {
  label_format(big.mark = ".", decimal.mark = ",", ...)
}

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = label_format_NL())

如果您不喜欢为每个轴指定标签,您可以像这样覆盖 ggplot2::continuous_scale ,例如就我

continuous_scale <- function(...) {
  do.call(
    ggplot2::continuous_scale, 
    c(list(labels = label_format_NL()), list(...))
  )
}

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

个人而言,我会使用显式的scale_AESTHETIC_continous(labels = label_format_NL() ) 因为这样可以使您的更改保持本地化。重写 options()continuous_scale() 在道德上等同于全局变量,不推荐。

A more general solution is to write a {scales} compatible labelling function label_format() that calls base::format() under the hood, and then to make a specialized version with the Dutch punctuation conventions.

library(ggplot2)
library(scales)

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

# Use base::format as a {scales}-compatible labels function
label_format <- function(...) {
  function(x) do.call(format, c(list(x = x), list(...)))
}

# Specialized version for Dutch punctuation
label_format_NL <- function(...) {
  label_format(big.mark = ".", decimal.mark = ",", ...)
}

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point() + 
  scale_x_continuous(labels = label_format_NL())

If you don't like having to specify the labels for every axis, you can override ggplot2::continuous_scale like this e.g.

continuous_scale <- function(...) {
  do.call(
    ggplot2::continuous_scale, 
    c(list(labels = label_format_NL()), list(...))
  )
}

ggplot(mtcars, aes(drat, mpg)) + 
  geom_point()

Personally, I'd use the explicit scale_AESTHETIC_continous(labels = label_format_NL()) since that keeps your changes localized. Overriding options() or continuous_scale() is the moral equivalent of global variables, not recommended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文