我有一个ggplot2
图,直方图如下所示:
library("ggplot2")
library("scales")
df <- data.frame(test = rnorm(1000, 1000, 40000))
p <- ggplot(df, aes(x = test)) + geom_histogram(colour="black", fill="#FF9999")
# set themes, axes and labels
p <- p + theme_minimal() + theme() + xlab("Distance travelled [km]") + ylab("Count of users")
# transformation
p <- p + scale_x_log10(breaks = trans_breaks("log10", function(x) {10^x}),
labels = trans_format("log10", math_format(10^.x))
)
# plot
p
输出:
我使用scales
包将x轴标签显示为10的幂,但实际上,我希望将它们显示为十进制数字,只要数字低于10^4和10^-4,对于下面的图,这将提供以下轴标签
100
1000
10^4
10^5
我如何在trans_format()中做到这一点,或者有其他更好的解决方案?
发布于 2013-06-03 22:11:01
您可以使用ifelse
在identity
和trans_format
之间进行分支
f <- function (x) ifelse(log10(x)<4,identity(x),trans_format("log10",math_format(10^.x))(x))
> f(10^(1:6))
[[1]]
[1] 10
[[2]]
[1] 100
[[3]]
[1] 1000
[[4]]
10^`4`
[[5]]
10^`5`
[[6]]
10^`6`
p + scale_x_log10(breaks = trans_breaks("log10", function(x) {10^x}), labels=f)
https://stackoverflow.com/questions/16895972
复制相似问题