我如何使用ggplot2和scale来格式化带有指数的轴标签? [英] How can I format axis labels with exponents with ggplot2 and scales?
问题描述
使用新版本的ggplot2和scale,我无法弄清楚如何以科学记数法获取轴标签。例如:
x < - 1:4
y < - c(0,0.0001,0.0002,0.0003 )
dd< - data.frame(x,y)
ggplot(dd,aes(x,y))+ geom_point()
给我
我希望轴标签为0,5 x 10 ^ -5,1×10 ^ -4,1.5×10 ^ -4等等。我找不到 scale_y_continuous()
和<$ c $的正确组合c> math_format()(至少我认为这些是我需要的)。
scale_y_log10()
log转换我不想要的轴。 scale_y_continuous(label = math_format())
只是给我10 ^ 0,10 ^ 5e-5等。我明白为什么后者会给出这个结果,但这不是我'b
$ b
我正在使用ggplot2_0.9.1和scales_0.2.1
简单地通过在scientific_10()函数中添加一个parse()(并将'x'更改为正确的'times'符号),最终得到:
x < - 1:4
y < - c(0,0.0001,0.0002,0.0003)
dd< - data.frame(x,y)
scientific_10 < - function(x){
parse(text = gsub(e, *),scale = scientific_format()(x)))
}
ggplot(dd,aes(x,y))+ geom_point()+ scale_y_continuous(label = scientific_10)
你可能仍然想要使这个函数更加智能化,所以它更优雅地处理0,但我认为就是这样!
With the new version ggplot2 and scales, I can't figure out how to get axis label in scientific notation. For example:
x <- 1:4
y <- c(0, 0.0001, 0.0002, 0.0003)
dd <- data.frame(x, y)
ggplot(dd, aes(x, y)) + geom_point()
gives me
I'd like the axis labels to be 0, 5 x 10^-5, 1 x 10^-4, 1.5 x 10^-4, etc. I can't figure out the correct combination of scale_y_continuous()
and math_format()
(at least I think those are what I need).
scale_y_log10()
log transforms the axis, which I don't want. scale_y_continuous(label = math_format())
just gives me 10^0, 10^5e-5, etc. I see why the latter gives that result, but it's not what I'm looking for.
I am using ggplot2_0.9.1 and scales_0.2.1
I adapted Brian's answer and I think I got what you're after.
Simply by adding a parse() to the scientific_10() function (and changing 'x' to the correct 'times' symbol), you end up with this:
x <- 1:4
y <- c(0, 0.0001, 0.0002, 0.0003)
dd <- data.frame(x, y)
scientific_10 <- function(x) {
parse(text=gsub("e", " %*% 10^", scales::scientific_format()(x)))
}
ggplot(dd, aes(x, y)) + geom_point()+scale_y_continuous(label=scientific_10)
You might still want to smarten up the function so it deals with 0 a little more elegantly, but I think that's it!
这篇关于我如何使用ggplot2和scale来格式化带有指数的轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!