Writing an -log10 transformation

53 views
Skip to first unread message

Freddie Witherden

unread,
Dec 7, 2011, 6:51:32 PM12/7/11
to ggplot2
Hello,

For one of my plots I wish for the axis to be negative logarithmic;
i.e, from 10^-2, 10^-3, ..., 10^-10. Sadly the ggplot2 coordinate
transforms do not seem to be stackable so:

scale_x_continuous(trans = c("log10","reverse"))

does not help. Hence I need to define my own transformation to
accomplish this. How can I go about doing this in a way that will
still leave me with nice axis labels and the such?

My attempt:

TransNegLog10 <- Trans$new("NegLog10", f = function(x) -log10(x),
inverse = function(x) 10^(-x), labels = function(x) bquote(10^.(-
log10(x))))

has been unsuccessful in the sense that labels are no longer 10^-2 etc
but rather 10^-<floating point>.

Regards, Freddie.

Hadley Wickham

unread,
Feb 10, 2012, 8:23:27 AM2/10/12
to Freddie Witherden, ggplot2
Hi Freddie,

You best bet is to use the development version of ggplot2, and then
read the help for creating new transformations in the scales package:

install.packages("devtools")
library(devtools)
dev_mode()
install_github("ggplot2")

library(ggplot2)
library(scales)
?trans

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Freddie Witherden

unread,
Feb 11, 2012, 10:18:01 AM2/11/12
to ggplot2
On Feb 10, 1:23 pm, Hadley Wickham <had...@rice.edu> wrote:
> install.packages("devtools")
> library(devtools)
> dev_mode()
> install_github("ggplot2")
>
> library(ggplot2)
> library(scales)
> ?trans

Thank you for this. The following worked a treat:
neglog_trans <- function (base = exp(1))
{
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
trans_new(str_c("neglog-", format(base)), trans, inv, log_breaks(base
= base), domain = c(1e-100, Inf))
}
neglog10_trans <- function () { neglog_trans(10) }

Regards, Freddie.
Reply all
Reply to author
Forward
0 new messages