强制R停止绘制缩写的轴标签 - 例如ggplot2中的1e + 00

JPD*_*JPD 82 r graph axes ggplot2

在ggplot2中,如何阻止轴标签缩写 - 例如1e+00, 1e+01,一旦绘制了x轴?理想情况下,我想强制R显示在这种情况下的实际值1,10.

任何帮助非常感谢.

Aru*_*run 110

我想你正在寻找这个:

require(ggplot2)
df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
# displays x-axis in scientific notation
p  <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p

# displays as you require
require(scales)
p + scale_x_continuous(labels = comma)
Run Code Online (Sandbox Code Playgroud)

  • 这工作了。谢谢。出于兴趣,带有刻度包的ggplot2中的轴还有哪些其他“标签”选项? (2认同)
  • 也请访问[此ggplot2.org页面](http://docs.ggplot2.org/current/scale_continuous.html),这对我解决类似问题非常有帮助。 (2认同)
  • 该链接已过时。现在你想看看 https://ggplot2-book.org/scale-position.html#label-functions - `scales::comma` 是 `scales::label_comma` 的简写,等等。 (2认同)
  • 唔; 只是尝试这个,我收到一个新错误:“错误:中断和标签的长度不同” (2认同)

jub*_*uba 54

你尝试过这样的事情:

options(scipen=10000)
Run Code Online (Sandbox Code Playgroud)

在策划之前?

  • 这通过设置更高的*惩罚*来决定使用科学记数法.在这个答案中有更多解释:http://stackoverflow.com/a/18600721/1080804 (2认同)

Der*_*ran 28

只是对@Arun所做的更新,因为我今天尝试了它并且它没有用,因为它实现了

+ scale_x_continuous(labels = scales::comma)
Run Code Online (Sandbox Code Playgroud)

  • @Arun 的答案应该可以正常工作,也许您忽略了包含“require(scales)”?这将导入包含“逗号”刻度的包。正如您所发现的,您还可以在引用包时指定该包,而不是事先要求它。 (3认同)

use*_*472 11

作为更通用的解决方案,您可以使用scales::format_format删除科学记数法.这也使您可以很好地控制标签显示的准确程度,scales::comma而不仅仅是数量级的逗号分隔.

例如:

require(ggplot2)
require(scales)
df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))

# Here we define spaces as the big separator
point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)

# Plot it
p  <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p + scale_x_continuous(labels = point)
Run Code Online (Sandbox Code Playgroud)

  • format_format 目前正在从包秤中重试。您应该使用 label_number() 或 label_date() 代替。 (4认同)

Wil*_*zyk 10

有一个不需要比例库的解决方案。

你可以试试:

# To deactivate scientific notation on y-axis:

    p + scale_y_continuous(labels = function(x) format(x, scientific = FALSE))

# To activate scientific notation on y-axis:

    p + scale_y_continuous(labels = function(x) format(x, scientific = TRUE))

# To deactivate scientific notation on x-axis:

    p + scale_x_continuous(labels = function(x) format(x, scientific = FALSE))

# To activate scientific notation on x-axis:

    p + scale_x_continuous(labels = function(x) format(x, scientific = TRUE))
Run Code Online (Sandbox Code Playgroud)


小智 5

将原始问题扩展为也包含分数,即 1、0.1、0.01、0.001 等,并避免尾随零

p + scale_x_continuous(labels = function(x) sprintf("%g", x))
Run Code Online (Sandbox Code Playgroud)

  • 从美学角度来看,这要好得多! (4认同)

Pet*_*ter 5

如果您还想使用逗号作为千位分隔符,可以使用以下命令:

p + scale_x_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
Run Code Online (Sandbox Code Playgroud)