log10 breaks and minor breaks

2,094 views
Skip to first unread message

baptiste auguie

unread,
Feb 9, 2012, 3:59:26 AM2/9/12
to ggplot2-dev
Hi,

From the very useful transition guide, I understand that the default
breaks have changed for scale_*_log10(). If I want the old behavior,
which I do, I need to write something like,

scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
labels = trans_format(’log10’, math_format(10^.x)))

which I don't find very convenient. Is there a shortcut?

Also, I find the minor breaks --half-way in between the major
breaks--, quite misleading / hard to interpret in ggplot2, being used
to seeing log-scales as in
http://upload.wikimedia.org/wikipedia/commons/b/b5/Logarithmic_Scales.svg
How do I get more customary minor breaks without too much hassle?

I'm unfamiliar with the new syntax, have I missed something?

Many thanks,

baptiste

baptiste auguie

unread,
Feb 9, 2012, 4:14:28 AM2/9/12
to ggplot2-dev
By the way, I also wanted to point out a missing link in the help for
?scale_x_continuous that suggests ?transformer which doesn't currently
exist. Do you know how to add a line note to a particular file on
github? I find I can only add such notes in the context of revisions,
I believe.

Best,

b.

On 9 February 2012 21:59, baptiste auguie

Hadley Wickham

unread,
Feb 9, 2012, 8:39:21 AM2/9/12
to baptiste auguie, ggplot2-dev
On Thu, Feb 9, 2012 at 2:59 AM, baptiste auguie
<baptist...@googlemail.com> wrote:
> Hi,
>
> From the very useful transition guide, I understand that the default
> breaks have changed for scale_*_log10(). If I want the old behavior,
> which I do, I need to write something like,
>
>    scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
>                            labels = trans_format(’log10’, math_format(10^.x)))
>
> which I don't find very convenient. Is there a shortcut?

scale_x_log10old <- function(...) {


scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),

labels = trans_format(’log10’, math_format(10^.x)), ...)
}

?

> Also, I find the minor breaks --half-way in between the major
> breaks--, quite misleading / hard to interpret in ggplot2, being used
> to seeing log-scales as in
> http://upload.wikimedia.org/wikipedia/commons/b/b5/Logarithmic_Scales.svg

The minor breaks should be half-way in between the major breaks in
data space, not in transformed space. Is that not what you're seeing?

> How do I get more customary minor breaks without too much hassle?

What are the more customary breaks?

Hadley

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

Hadley Wickham

unread,
Feb 9, 2012, 8:40:18 AM2/9/12
to baptiste auguie, ggplot2-dev
> By the way, I also wanted to point out a missing link in the help for
> ?scale_x_continuous that suggests ?transformer which doesn't currently
> exist. Do you know how to add a line note to a particular file on
> github? I find I can only add such notes in the context of revisions,
> I believe.

Best to file a bug report - there is no guarantee I will remember to
follow up on a line note. (And the ref should be to ?scales::trans.

baptiste auguie

unread,
Feb 9, 2012, 6:32:15 PM2/9/12
to Hadley Wickham, ggplot2-dev
Hi,

On 10 February 2012 02:39, Hadley Wickham <had...@rice.edu> wrote:
> On Thu, Feb 9, 2012 at 2:59 AM, baptiste auguie
> <baptist...@googlemail.com> wrote:
>> Hi,
>>
>> From the very useful transition guide, I understand that the default
>> breaks have changed for scale_*_log10(). If I want the old behavior,
>> which I do, I need to write something like,
>>
>>    scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
>>                            labels = trans_format(’log10’, math_format(10^.x)))
>>
>> which I don't find very convenient. Is there a shortcut?
>
> scale_x_log10old <- function(...) {
>  scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
>    labels = trans_format(’log10’, math_format(10^.x)), ...)
> }
>
> ?

Could this be included in ggplot2? In fact, since scale_x_log10() now
appears a little redundant, given the more general
scale_continuous(trans=""), could we make it a convenience function
that selects either style?

scale_x_log10 <- function(..., style=c("original", "transformed")) {

style <- match.arg(style)

breaks <- switch(style, "original"=NULL,
"transformed"=trans_breaks(’log10’, function(x) 10^x))
labels <- switch(style, "original"=NULL,
"transformed"=trans_format(’log10’, math_format(10^.x)))
dots <- list(...)

params <- modifyList(dots, list(trans = log10_trans(),
breaks=breaks, labels=labels))
do.call(scale_x_continuous, params)

}

set.seed(123)
DF <- data.frame(x = rlnorm(1000, m = 1, sd = 0.2), y=rnorm(1000))

p <- ggplot(DF, aes(x = x, y=y)) + geom_point()

library(gridExtra)
grid.arrange(
p + scale_x_continuous(trans = "log10"),
p + scale_x_log10(style="original"), # equivalent to the previous one
p + scale_x_log10(style="transformed")) # old style

In all of the above the minor_breaks are in the midposition between
breaks; in fact I was not successful in changing them at all with

minor_breaks=trans_breaks("log10", function(x) 10^x, n=100)

What I consider the most common case for minor breaks is shown here
(first few graphs anyway): http://en.wikipedia.org/wiki/Semi-log_plot

Schematically,

| : : : ::::| : : : ::::| : : : ::::|
1 10 100 1000

Thanks,

baptiste

Screen shot 2012-02-10 at 12.29.37 PM.png

Winston Chang

unread,
Feb 9, 2012, 9:06:01 PM2/9/12
to baptiste auguie, Hadley Wickham, ggplot2-dev
> scale_x_log10old <- function(...) {
>  scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
>    labels = trans_format(’log10’, math_format(10^.x)), ...)
> }
>
> ?

Could this be included in ggplot2? In fact, since scale_x_log10() now
appears a little redundant, given the more general
scale_continuous(trans=""), could we make it a convenience function
that selects either style?

scale_x_log10 <- function(..., style=c("original", "transformed")) {

I think this (or something like it) is a good idea. The current method is very complicated from a user perspective.

 
Schematically,

 |          :     :  : ::::|          :     :  : ::::|          :     :  : ::::|
1                        10                      100                    1000

I have some code to generate diminishing tick marks like these using geom_segment (although they appear on the inside of the plotting area rather than the outside). The correct long term solution would be to put this in the guides, but if there's an immediate demand for this, it wouldn't be hard to write a geom that does this, similar to geom_rug. Maybe it could go in ggExtra? (By the way, is that package still around?)

-Winston

Kohske Takahashi

unread,
Feb 10, 2012, 12:07:04 PM2/10/12
to Winston Chang, baptiste auguie, Hadley Wickham, ggplot2-dev
I'm not sure if I can follow, but now you can do:

qplot(10^(1:6), 10^(1:6)) + scale_x_log10(breaks = 10^(1:6),
minor_breaks = log(c(sapply(x, function(x) seq(0, x, x/10))), 10)

I think the inside tick is useful and will appear with the improvement
of positional guides.

As for the original and transformed spaces, see also this issue:
https://github.com/hadley/ggplot2/issues/373

kohske

2012/2/10 Winston Chang <winsto...@gmail.com>:

--
--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Hadley Wickham

unread,
Feb 10, 2012, 12:54:10 PM2/10/12
to baptiste auguie, ggplot2-dev
>> scale_x_log10old <- function(...) {
>>  scale_x_log10(breaks = trans_breaks(’log10’, function(x) 10^x),
>>    labels = trans_format(’log10’, math_format(10^.x)), ...)
>> }
>>
>> ?
>
> Could this be included in ggplot2? In fact, since scale_x_log10() now
> appears a little redundant, given the more general
> scale_continuous(trans=""), could we make it a convenience function
> that selects either style?
>
> scale_x_log10 <- function(..., style=c("original", "transformed")) {
>
>  style <- match.arg(style)
>
>  breaks <- switch(style, "original"=NULL,
> "transformed"=trans_breaks(’log10’, function(x) 10^x))
>  labels <- switch(style, "original"=NULL,
> "transformed"=trans_format(’log10’, math_format(10^.x)))
>  dots <- list(...)
>
>  params <-  modifyList(dots, list(trans = log10_trans(),
> breaks=breaks, labels=labels))
>  do.call(scale_x_continuous, params)
>
> }

I like that idea! I think the details need a little tweaking (so you
can still specify breaks and labels in the same way as other scales),
but smart details would be better. I don't think that needs much
tweaking:

breaks <- breaks %||% switch(style, "trans" = trans_breaks(’log10’,
function(x) 10^x))
labels <- labels %||% switch(style, "trans" = trans_format(’log10’,
math_format(10^.x)))

(I'd prefer adding breaks and labels to using your perfectly valid
technique of modifying dots because it makes tracebacks easier to
understand if something goes wrong).

And maybe style = c("raw", "trans") or c("data", "trans"). Not sure.

> In all of the above the minor_breaks are in the midposition between
> breaks; in fact I was not successful in changing them at all with
>
> minor_breaks=trans_breaks("log10", function(x) 10^x, n=100)

That may be a bug. Would you mind filing a bug report?

> What I consider the most common case for minor breaks is shown here
> (first few graphs anyway): http://en.wikipedia.org/wiki/Semi-log_plot
>
> Schematically,
>
>  |          :     :  : ::::|          :     :  : ::::|          :     :  : ::::|
> 1                        10                      100                    1000

I'm certainly open to better defaults, but it's really hard to get
them to work in a wide variety of situations. e.g. what should the
breaks and minor breaks look like if the range is 2-5 ? Some test
cases would be really useful (and I think if someone really spent time
thinking about this it would be publishable - especially if you could
extend to other non-linear scales)

Reply all
Reply to author
Forward
0 new messages