随着新版本GGPLOT2和秤,我无法弄清楚如何让科学记数法轴标签。 例如:
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×10 ^ -5,1×10 ^ -4,1.5×10 ^ -4等我不能找出的正确组合scale_y_continuous()
和math_format()
(至少我认为这些都是我所需要的)。
scale_y_log10()
日志转换轴,这是我不想要的。 scale_y_continuous(label = math_format())
只是给了我10 ^ 0,10 ^ 5E-5等我明白为什么后者给出的结果,但它不是我要找的。
我使用ggplot2_0.9.1和scales_0.2.1
我适应Brian的回答,我想我得到了你以后。
只需添加一个解析()与scientific_10()函数(和不断变化的“X”为正确的“次”符号),你结束了这一点:
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)
你可能还是要变得聪明起来的功能,所以它与0多一点优雅的交易,但我认为这就是它!
按上接受的解决方案的意见,OP正在格式化指数作为指数。 这可以用做trans_format
和trans_breaks
在秤包装功能:
library(ggplot2)
library(scales)
x <- 1:4
y <- c(0, 0.0001, 0.0002, 0.0003)
dd <- data.frame(x, y)
ggplot(dd, aes(x, y)) + geom_point() +
scale_y_log10("y",
breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
scale_y_continuous(label=scientific_format())
给使用E而不是10个标签:
我想,如果你真的想10的在那里,你可以再包装,在其他功能。
scientific_10 <- function(x) {
gsub("e", " x 10^", scientific_format()(x))
}
ggplot(dd, aes(x, y)) + geom_point() +
scale_y_continuous(label=scientific_10)
我写了一个版本scientific_10避免鳞包; 它也除去在指数前导零(10 ^ 04至10 ^ 4,等等)。 这是改编自上面给出的有用的答案。
我还包含以下包装秤功能。
scientific_10 <- function(x) {
xout <- gsub("1e", "10^{", format(x),fixed=TRUE)
xout <- gsub("{-0", "{-", xout,fixed=TRUE)
xout <- gsub("{+", "{", xout,fixed=TRUE)
xout <- gsub("{0", "{", xout,fixed=TRUE)
xout <- paste(xout,"}",sep="")
return(parse(text=xout))
}
scale_x_log10nice <- function(name=NULL,omag=seq(-10,20),...) {
breaks10 <- 10^omag
scale_x_log10(name,breaks=breaks10,labels=scientific_10(breaks10),...)
}
scale_y_log10nice <- function(name=NULL,omag=seq(-10,20),...) {
breaks10 <- 10^omag
scale_y_log10(name,breaks=breaks10,labels=scientific_10(breaks10),...)
}
scale_loglog <- function(...) {
list(scale_x_log10nice(...),scale_y_log10nice(...))
}
qplot(x=exp(5*rnorm(100)),geom="density",kernel="rectangular") +
scale_x_log10nice()
Riffing远,汤姆的回答上面,下面移除了+标志,并处理好0(功能被匿名内联和):
scale_y_continuous(label= function(x) {ifelse(x==0, "0", parse(text=gsub("[+]", "", gsub("e", " %*% 10^", scientific_format()(x)))))} ) +