I have found coord_trans
, but I\'d like to apply log10
and
reverse
to my x-axis. I tried applying two transformation
ggplo
A quick and easy way is to apply one of the transformations directly to the data and use the other with the plot function.
e.g.
ggplot(iris, aes(log10(Sepal.Length), log10(Sepal.Width), colour = Species)) +
geom_point() + coord_trans(x="reverse", y="reverse")
Note: the reverse transformation does not work with the iris data but you get the idea.
I wandered in here looking for a 'composition of scales' function. I think one might be able to write such a thing as follows:
# compose transforms a and b, applying b first, then a:
`%::%` <- function(atrans,btrans) {
mytran <- scales::trans_new(name = paste(btrans$name,'then',atrans$name),
transform = function(x) { atrans$transform(btrans$transform(x)) },
inverse = function(y) { btrans$inverse(atrans$inverse(y)) },
domain = btrans$domain, # this could use improvement...
breaks = btrans$breaks, # not clear how this should work, tbh
format = btrans$format)
}
ph <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
scale_y_continuous(trans=scales::reverse_trans() %::% scales::log10_trans())
print(ph)
You can define new transformations using trans_new
.
library(scales)
log10_rev_trans <- trans_new(
"log10_rev",
function(x) log10(rev(x)),
function(x) rev(10 ^ (x)),
log_breaks(10),
domain = c(1e-100, Inf)
)
p <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
p + coord_trans(y = log10_rev_trans)