I'd like to make a plot with a reversed, log10 x scale using ggplot2:
require(ggplot2)
df <- data.frame(x=1:10, y=runif(10))
p <- ggplot(data=df, aes(x=x, y=y)) + geom_point()
However, it seems that I can either a log10 scale or a reversed scale:
p + scale_x_reverse() + scale_x_log10()
p + scale_x_reverse()
I guess this is logical, if a layer can only have one scale. And certainly I could hack it by doing the log transform on the dataframe myself, df$xLog <- log10(df$x)
but that solution is a seems contrary to the spirit of ggplot. Is there a way to get this kind of plot without doing data transformations external to the ggplot call?
Best Answer
The link that @joran gave in his comment gives the right idea (build your own transform), but is outdated with regard to the new
scales
package thatggplot2
uses now. Looking atlog_trans
andreverse_trans
in the scales package for guidance and inspiration, areverselog_trans
function can be made:This can be used simply as:
which gives the plot:
Using a slightly different data set to show that the axis is definitely reversed: