formatting of labels in legends from trans_format

186 views
Skip to first unread message

Thomas

unread,
Jun 25, 2012, 9:26:35 AM6/25/12
to ggp...@googlegroups.com
Hi everyone

I'm having trouble formatting the labels in legends when inversing a transformation. As an example, consider the following:

test <- data.frame(expand.grid(1:10, 1:10), value=runif(100, min=1, max=1000))


p <- qplot(Var1, Var2, data=test, geom='tile', fill=value)


If I where to log transform the fill scale and furthermore do something to the labels and breaks, I would add

p + scale_fill_gradient(trans='log10', guide=guide_colorbar(), breaks=trans_breaks('log10', function(x) 10^x), labels=trans_format('log10', math_format()))


The problem is that the labels are plotted as unevaluated calls and thus not very pretty... I have tried different permutations of using eval(), parse() and expression() around trans_format() without luck... Is it not possible to put expression as legend labels when dealing with scale transformations?

best wishes

Thomas

Brandon Hurr

unread,
Jun 25, 2012, 10:01:21 AM6/25/12
to Thomas, ggp...@googlegroups.com
I was playing and it looks like the problem is as.name(). It puts single backwards quotes around the number which do not get parsed. When I took it out it seemed to work ok... check attachment. 

>math_format
function (expr = 10^.x, format = force) 
{
    quoted <- substitute(expr)
    subs <- function(x) {
        do.call("substitute", list(quoted, list(.x = as.name(x))))
    }
    function(x) {
        x <- format(x)
        llply(x, subs)
    }
}
<environment: namespace:scales>
file.png

Thomas

unread,
Jun 25, 2012, 10:17:35 AM6/25/12
to ggp...@googlegroups.com
Thats definitely an improvement, but I had hoped it was possible to plot actual expressions, as it is with position scales... Is this not possible for legend labels?

best

Thomas

Brandon Hurr

unread,
Jun 25, 2012, 11:24:06 AM6/25/12
to Thomas, ggp...@googlegroups.com
Yes, sorry, I realize what you're saying now. It's still not parsing the ^ into an exponent. If you look at the guts, I don't see where it's being told to parse it as an expression at all. 

> tf <- trans_format("log10", math_format())
> tf(10 ^ 1:6)
[[1]]
10^`1`

[[2]]
10^`0.9542425`

[[3]]
10^`0.90309`

[[4]]
10^`0.845098`

[[5]]
10^`0.7781513`

> trans_format
function (trans, format = scientific_format())
{
   if (is.character(trans))
       trans <- match.fun(trans)
   function(x) {
       x <- trans(x)
       format(x)
   }
}
<environment: namespace:scales>

> math_format
function (expr = 10^.x, format = force)
{
   quoted <- substitute(expr)
   subs <- function(x) {
       do.call("substitute", list(quoted, list(.x = as.name(x))))
   }
   function(x) {
       x <- format(x)
       llply(x, subs)
   }
}
<environment: namespace:scales>

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

Kohske Takahashi

unread,
Jun 25, 2012, 8:49:43 PM6/25/12
to Brandon Hurr, Thomas, ggp...@googlegroups.com
This is a bug: https://github.com/hadley/ggplot2/pull/594
Thanks.

kohske

2012/6/26 Brandon Hurr <brando...@gmail.com>:
--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
Reply all
Reply to author
Forward
0 new messages