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

我使用R和ggplot来绘制一些数据的散点图,除了y轴上的数字出现在计算机样式指数格式中,例如4e + 05,5e + 05等,都是很好的。不可接受的,所以我想把它们显示为50万,40万,等等。 获得正确的指数符号也是可以接受的。

该图的代码如下所示:

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") 

任何帮助非常感谢。

另一种select是用逗号格式化你的轴刻度标签是通过使用包scales ,并添加

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

到你的ggplot声明。

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

 scale_y_continuous(name="Fluorescent intensity/arbitrary units", labels = scales::comma) 
 x <- rnorm(10) * 100000 y <- seq(0, 1, length = 10) p <- qplot(x, y) library(scales) p + scale_x_continuous(labels = comma) 

我也发现了另一种做法,在轴上给出适当的'x10(上标)5'符号。 我在这里发布,希望对一些人有用。 我从这里得到的代码,所以我声称没有信用,这正确地去布莱恩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) 

我在这里迟到了,但是在别人想要一个简单的解决scheme的情况下,我创build了一组函数,可以这样调用:

  ggplot + scale_x_continuous(labels = human_gbp) 

它给你人类可读的x或y轴的数字(或一般真的)。

你可以在这里find函数: Github Repo只需将函数复制到脚本中,以便调用它们。

我发现杰克·艾德利的build议答案是一个有用的答案。

我想抛出另一种select。 假设你有一个有很多小数字的系列,你要确保轴标签写出完整的小数点(例如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)