Reputation: 2974
I have a ggplot2
plot,histogram produced as follows:
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
Output:
I used the scales
package to display x axis labels as powers of ten, but actually, I would like to display them as decimal numbers, as long as the number is below, let's say 10^4 and 10^-4, for the below plot, this would give the following axis labels
100
1000
10^4
10^5
How can I do this within trans_format(), or is there another, better solution?
Upvotes: 3
Views: 1491
Reputation: 66834
You can use ifelse
to branch between identity
and 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)
Upvotes: 3