如何使用ggplot更改轴上数字的格式?

164

我正在使用R和ggplot绘制一些数据的散点图,所有的都很好,除了y轴上的数字以计算机科学的指数格式出现,即4e+05、5e+05等。这对我来说是不可接受的,因此我想要将它们显示为500,000、400,000等等。获取适当的指数表示法也可以接受。

绘图代码如下:

p <- ggplot(valids, aes(x=Test, y=Values)) +
  geom_point(position="jitter") +
  facet_grid(. ~ Facet) +
  scale_y_continuous(name="Fluorescent intensity/arbitrary units") +
  scale_x_discrete(name="Test repeat") +
  stat_summary(fun.ymin=median, fun.ymax=median, fun.y=median, geom="crossbar")

非常感谢任何帮助。


40
小心描述 ggplot 默认选项为“显然不可接受”。你的意思是你个人偏好不同的格式。格式为4e+05 的数字是科学计数法,在许多应用程序中都是首选格式。 - Andrie
5个回答

168
另一个选项是使用包scales格式化您的轴刻度标签,并添加逗号。
 scale_y_continuous(name="Fluorescent intensity/arbitrary units", labels = comma)

添加到您的 ggplot 语句中。

如果您不想加载该软件包,请使用:

scale_y_continuous(name="Fluorescent intensity/arbitrary units", labels = scales::comma)

16
这么微不足道的问题需要加载一个新的软件包,真是令人惊讶。 - luchonacho
顺便提一下,这也适用于scale_y_log10(labels = scales::comma),我假设ggplot2中的其他比例尺也是如此。非常好的技巧! - TheProletariat
7
虽然有些晚了,但您可以将任何函数传递给标签参数,因此 scale_x_continuous(labels = function(x) format(x, big.mark = ",")) - user1697590

83

我还发现了另一种方法可以在坐标轴上正确显示“x10(superscript)5”的符号。我将其发布在这里,希望对一些人有用。我从这里获取了代码,因此我不对此做出任何贡献,因为这属于Brian Diggs。

fancy_scientific <- function(l) {
     # turn in to character string in scientific notation
     l <- format(l, scientific = TRUE)
     # quote the part before the exponent to keep all the digits
     l <- gsub("^(.*)e", "'\\1'e", l)
     # turn the 'e+' into plotmath format
     l <- gsub("e", "%*%10^", l)
     # return this as an expression
     parse(text=l)
}

然后您可以将其用作

ggplot(data=df, aes(x=x, y=y)) +
   geom_point() +
   scale_y_continuous(labels=fancy_scientific) 

11
如果您不希望将0打印为 "0 x 10⁺⁰",请在 format(...) 行下方添加以下内容: l <- gsub("0e\\+00","0",l) - semi-extrinsic
1
如果你想特殊处理其他内容,最简单的方法是在format()之后直接添加更多的gsub(),同时在单独的控制台中测试format()返回的结果。 - semi-extrinsic
5
在最后一个gsub命令之前加入以下内容: #如果存在,则删除指数后面的+,例如:(3x10 ^ + 2 - > 3x10 ^ 2) l <- gsub("e \\ +“,” e “,l)在此之后加入以下内容: #将1x10 ^或1.000x10 ^转换为10 ^,以使其符合论文通常使用的格式。 l <- gsub("\\'1 [\\.0] * \\'\\% \\* \\%", "",l) - John_West
在此回答中提出了一个后续问题:https://stackoverflow.com/questions/63477686/how-do-i-make-ggplot2-custom-text-formats-from-axis-scale-functions-follow-forma - Jordan Mandel

47
x <- rnorm(10) * 100000
y <- seq(0, 1, length = 10)
p <- qplot(x, y)
library(scales)
p + scale_x_continuous(labels = comma)

当我尝试这个时,出现了一个错误,说格式化程序是未使用的参数?它需要另一个包或其他东西吗? - Jack Aidley
4
وˆ‘ن؟®و”¹ن؛†ن»£ç پن»¥هŒ…هگ«library(scales)ه¹¶ن½؟用comma,è؟™ه؛”该و¯”وˆ‘ن¹‹ه‰چن½؟用çڑ„ه‡½و•°و•ˆو‍œو›´ه¥½م€‚ - DiscreteCircle

18

我来晚了,但如果其他人想要简单的解决方案,我创建了一组可以像这样调用的函数:

 ggplot + scale_x_continuous(labels = human_gbp)

这些函数可以让你的x或y轴(或任何数字)更加易读。

你可以在这里找到这些函数:Github Repo 只需将这些函数复制到你的脚本中,这样就可以调用它们。


12

我认为杰克·艾德利提出的建议很有用。

我想提供另一种选择。假设您拥有一个包含许多小数的系列,并且希望确保轴标签写出完整的小数点(例如5e-05 -> 0.0005),则:

NotFancy <- function(l) {
 l <- format(l, scientific = FALSE)
 parse(text=l)
}

ggplot(data = data.frame(x = 1:100, 
                         y = seq(from=0.00005,to = 0.0000000000001,length.out=100) + runif(n=100,-0.0000005,0.0000005)), 
       aes(x=x, y=y)) +
     geom_point() +
     scale_y_continuous(labels=NotFancy) 

35
可以使用匿名函数来缩短代码:scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})。为什么没有预定义的格式化程序,谁知道呢。 - eMPee584
3
@eMPee584 抱歉打扰了这个旧的回复,但请考虑将其作为答案发布,这样可以更容易地被找到 :) 您的评论得到的赞数比一些实际答案还要多。 - ByteHamster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接