如何使用ggplot更改坐标轴上的数字格式? [英] How do I change the formatting of numbers on an axis with ggplot?

查看:404
本文介绍了如何使用ggplot更改坐标轴上的数字格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R和ggplot绘制了一些数据的散点图,除了y轴上的数字出现在计算机样式指数格式中,例如4e + 05,5e + 05等等之外,一切都很好。这显然是不可接受的,所以我想让它显示为500,000,400,000等等。获得适当的指数记法也是可以接受的。

I'm using R and ggplot to draw a scatterplot of some data, all is fine except that the numbers on the y-axis are coming out with computer style exponent formatting, i.e. 4e+05, 5e+05, etc. This is obviously unacceptable, so I want to get it to display them as 500,000, 400,000, and so on. Getting a proper exponent notation would also be acceptable.

剧情的代码如下:

The code for the plot is as follows:

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

非常感谢任何帮助。

推荐答案

我还发现了另一种做法,在轴上给出了正确的'x10(上标)5'符号。我在这里发布它,希望对一些人有用。我从这里获得了代码,所以我声称不会赞扬它,这正确地转到Brian Diggs

I also found another way of doing this that gives proper 'x10(superscript)5' notation on the axes. I'm posting it here in the hope it might be useful to some. I got the code from here so I claim no credit for it, that rightly goes to 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)
}

其中您可以使用作为

Which you can then use as

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

这篇关于如何使用ggplot更改坐标轴上的数字格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆