是否有一个选项可以设置“,”作为 ggplot 中轴标签的默认小数点?
制作图表时,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并非特定于 ggplot2,但您可以使用
options(OutDec = ",")
全局设置输出小数点字符。从帮助页面:
Not specific to
ggplot2
but you can set the output decimal point character globally usingoptions(OutDec = ",")
.From the help page:
关键似乎是
format
(来自基础 R)和scales::number
使用不同的规则。我们可以恢复使用format
...如果您想让这些标签成为全局默认标签,我认为您可以这样做:
The key seems to be that
format
(from base R) andscales::number
use different rules. We can revert to usingformat
...If you want to make these labels the global default I think you can do this:
更通用的解决方案是编写一个与
{scales}
兼容的标签函数label_format()
在底层调用base::format()
,然后根据荷兰标点符号惯例制作一个专门的版本。如果您不喜欢为每个轴指定标签,您可以像这样覆盖 ggplot2::continuous_scale ,例如就我
个人而言,我会使用显式的scale_AESTHETIC_continous(labels = label_format_NL() ) 因为这样可以使您的更改保持本地化。重写
options()
或continuous_scale()
在道德上等同于全局变量,不推荐。A more general solution is to write a
{scales}
compatible labelling functionlabel_format()
that callsbase::format()
under the hood, and then to make a specialized version with the Dutch punctuation conventions.If you don't like having to specify the labels for every axis, you can override
ggplot2::continuous_scale
like this e.g.Personally, I'd use the explicit
scale_AESTHETIC_continous(labels = label_format_NL())
since that keeps your changes localized. Overridingoptions()
orcontinuous_scale()
is the moral equivalent of global variables, not recommended.