log10 axes

142 views
Skip to first unread message

Ben Bolker

unread,
Jun 29, 2011, 2:06:49 PM6/29/11
to ggplot2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Just wondering if there was any update on the possibility of plotting
log-scaled axes without scientific notations, and with breaks that are
pretty on the original scale, as in these threads:

http://groups.google.com/group/ggplot2/browse_thread/thread/658aac9db56065b/fcf69c35ae0898e4?lnk=gst&q=scale_y_log10+breaks+pretty#fcf69c35ae0898e4

http://groups.google.com/group/ggplot2/browse_thread/thread/f0a5d89133c600ed/3a820d0ab05e5d8d?lnk=gst&q=scale_y_log10+formatter#3a820d0ab05e5d8d


This is my current solution, which could probably be tweaked a bit:
the guts are mostly drawn from the example in ?axTicks which replicates
the internal calculations that base R does for pretty log axis ticks ...

lsc <- function(limit,...) {
get_axp <- function(x) 10^c(ceiling(x[1]), floor(x[2]))
## mimic par("yaxs")=="i"
usr.i <- log10(limit)
(aT.i <- axTicks(side=2, usr=usr.i,
axp=c(get_axp(usr.i), n=3), log=TRUE))
scale_y_log10(breaks=aT.i,labels=format(aT.i,scientific=FALSE,
trim=TRUE, drop0trailing=TRUE))
}

??
Ben Bolker
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4LabkACgkQc5UpGjwzenNnvgCglhbA+HdQdcR4yU3gPztgDcn7
d6wAmwb+c7X1mBmVFtISYE/43dZhWvIo
=epB2
-----END PGP SIGNATURE-----

Hadley Wickham

unread,
Jun 29, 2011, 2:20:53 PM6/29/11
to Ben Bolker, ggplot2
>  Just wondering if there was any update on the possibility of plotting
> log-scaled axes without scientific notations, and with breaks that are
> pretty on the original scale, as in these threads:
>
> http://groups.google.com/group/ggplot2/browse_thread/thread/658aac9db56065b/fcf69c35ae0898e4?lnk=gst&q=scale_y_log10+breaks+pretty#fcf69c35ae0898e4
>
> http://groups.google.com/group/ggplot2/browse_thread/thread/f0a5d89133c600ed/3a820d0ab05e5d8d?lnk=gst&q=scale_y_log10+formatter#3a820d0ab05e5d8d

Yes, this will be the default in the next version of ggplot2.

If you want to try out the latest version, it's not too difficult to do:

install.packages("devtools")
library(devtools)

install_github("devtools") # get latest dev version of devtools
# may need to restart R here (not sure)
install_github("scales") # get new scales package

dev_mode() # switch to dev mode so you don't kill your existing install
install_github("ggplot2", branch = "develop")

library(ggplot2)
qplot(mpg, wt, data = mtcars) + scale_x_log10()

(you'll need R 2.13, and if on windows, you'll need to have installed Rtools)

If it doesn't work, please let me know so I can fix the instructions -
I would like to make it easier for people to try out the development
version.

Hadley


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

Brian Diggs

unread,
Jun 29, 2011, 3:17:10 PM6/29/11
to Ben Bolker, ggplot2


On 6/29/2011 11:20 AM, Hadley Wickham wrote:
>> Just wondering if there was any update on the possibility of plotting
>> log-scaled axes without scientific notations, and with breaks that are
>> pretty on the original scale, as in these threads:
>>
>> http://groups.google.com/group/ggplot2/browse_thread/thread/658aac9db56065b/fcf69c35ae0898e4?lnk=gst&q=scale_y_log10+breaks+pretty#fcf69c35ae0898e4
>>
>> http://groups.google.com/group/ggplot2/browse_thread/thread/f0a5d89133c600ed/3a820d0ab05e5d8d?lnk=gst&q=scale_y_log10+formatter#3a820d0ab05e5d8d
>
> Yes, this will be the default in the next version of ggplot2.
>
> If you want to try out the latest version, it's not too difficult to do:

And if you want a hack that works with the current version (and is
pretty much guaranteed to not work with the next one):

TransLog10I <- Trans$new("log10I", "log10",
function(x) 10^x, function(x) format(10^x, scientific=FALSE))

ggplot(...) +
... +
scale_y_continuous(trans="log10I")


> install.packages("devtools")
> library(devtools)
>
> install_github("devtools") # get latest dev version of devtools
> # may need to restart R here (not sure)
> install_github("scales") # get new scales package
>
> dev_mode() # switch to dev mode so you don't kill your existing install
> install_github("ggplot2", branch = "develop")
>
> library(ggplot2)
> qplot(mpg, wt, data = mtcars) + scale_x_log10()
>
> (you'll need R 2.13, and if on windows, you'll need to have installed Rtools)
>
> If it doesn't work, please let me know so I can fix the instructions -
> I would like to make it easier for people to try out the development
> version.
>
> Hadley
>
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University


peta

unread,
Jun 30, 2011, 10:46:45 AM6/30/11
to ggp...@googlegroups.com, Ben Bolker
Thanks for these instructions. I'd like to try the new version.
Unfortunately it is not seamless and fails in install_githup.  
Looks like it can't find 'wget'

R version 2.13.0 (2011-04-13)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i386-pc-mingw32/i386 (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
  Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> install.packages("devtools")
--- Please select a CRAN mirror for use in this session ---
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.13
trying URL 'http://cran.parentingamerica.com/bin/windows/contrib/2.13/devtools_0.1.zip'
Content type 'application/zip' length 60853 bytes (59 Kb)
opened URL
downloaded 59 Kb
package 'devtools' successfully unpacked and MD5 sums checked
The downloaded packages are in
        C:\Users\pwhaite\AppData\Local\Temp\RtmpA0eAUh\downloaded_packages
> library(devtools)
> install_github("devtools")
Installing devtools from hadley
Error in unzip(src, list = TRUE) : 
  zip file 'C:\Users\pwhaite\AppData\Local\Temp\RtmpA0eAUh/hadley-devtools.zip' cannot be opened
In addition: Warning messages:
1: running command 'wget --quiet https://nodeload.github.com/hadley/devtools/zipball/master -O C:\Users\pwhaite\AppData\Local\Temp\RtmpA0eAUh/hadley-devtools.zip' had status 1 
2: In download.file(zip_url, src, method = "wget", quiet = TRUE) :
  download had nonzero exit status

Hadley Wickham

unread,
Jun 30, 2011, 2:13:53 PM6/30/11
to ggp...@googlegroups.com, Ben Bolker
That implies you don't have the latest version of devtools - you want
at least 0.2
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

John Owens

unread,
Jul 9, 2011, 1:27:23 AM7/9/11
to ggplot2
Hi Hadley, I'm following the directions on MacPorts R 2.13.0 and
having no luck. Transcript attached.

nanaimo-bar 9413$ R

R version 2.13.0 (2011-04-13)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin10.7.0/x86_64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> install.packages("devtools")
Installing package(s) into '/Users/jowens/Library/R/2.5'
(as 'lib' is unspecified)
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done
trying URL 'http://cran.cnr.Berkeley.edu/src/contrib/
devtools_0.3.tar.gz'
Content type 'application/x-gzip' length 14588 bytes (14 Kb)
opened URL
==================================================
downloaded 14 Kb

* installing *source* package 'devtools' ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (devtools)

The downloaded packages are in
'/private/var/folders/tI/tIJfPkuHHj0VUt3TaBn9G++++TI/-Tmp-/RtmpJbAKPI/
downloaded_packages'
> library(devtools)
> install_github("devtools")
Loading required package: RCurl
Installing devtools from hadley
Error in install_github("devtools") :
could not find function "getBinaryURL"
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE,
logical.return = TRUE, :
there is no package called 'RCurl'

John Owens

unread,
Jul 9, 2011, 1:30:08 AM7/9/11
to ggplot2
On Jul 8, 10:27 pm, John Owens <jow...@ece.ucdavis.edu> wrote:
> Hi Hadley, I'm following the directions on MacPorts R 2.13.0 and
> having no luck. Transcript attached.

Um, didn't have RCurl installed. Installing that fixed things. Sorry!

JDO

David Morrison

unread,
Jul 15, 2011, 12:34:01 PM7/15/11
to ggp...@googlegroups.com, Ben Bolker
I've been experimenting with log10 axes and the formatting of labels using the develop branch.  I can get close to what I want with the following:

print(qplot(c(1,10),c(1,10)) +
scale_y_log10(labels=trans_format("log10",math_format(format=scientific_format(2)))) +
scale_x_log10(labels=trans_format("log10",math_format(format=scientific_format(2)))))

It's a bit wordy, but I think I understand what each element of the label formatting is doing.  I would like to write a function to do the formatting I want.  I tried this:

exp_format <- function() {
  function(x) {
trans_format("log10",math_format(format=scientific_format(x,digits=2,...)))
  }
}

ef <- exp_format()

print(qplot(c(1,10),c(1,10)) + scale_y_log10(labels=ef) + scale_x_log10(labels=ef))

But this gives me the following error:

Error in scale_labels.continuous(scale) : 
  Breaks and labels are different lengths

Clearly, I am not understanding something basic here.  

Hadley Wickham

unread,
Jul 15, 2011, 1:10:15 PM7/15/11
to ggp...@googlegroups.com, Ben Bolker
On Fri, Jul 15, 2011 at 11:34 AM, David Morrison <da...@bnl.gov> wrote:
> I've been experimenting with log10 axes and the formatting of labels using
> the develop branch.  I can get close to what I want with the following:
> print(qplot(c(1,10),c(1,10)) +
> scale_y_log10(labels=trans_format("log10",math_format(format=scientific_format(2))))
> +
> scale_x_log10(labels=trans_format("log10",math_format(format=scientific_format(2)))))
> It's a bit wordy, but I think I understand what each element of the label
> formatting is doing.

Great! I'm trying to make things more explicit, which does mean a bit
more typing, but I think the tradeoff is worth it.

I would like to write a function to do the formatting
> I want.  I tried this:
> exp_format <- function() {
>   function(x) {
> trans_format("log10",math_format(format=scientific_format(x,digits=2,...)))
>   }
> }

You weren't using x in quite the right place. You still need to call
the function that trans_format produces:

exp_format <- function() {
function(x) {
exponent <- math_format(format = scientific_format(digits = 2))
trans_format("log10", exponent)(x)
}
}

Or you can get rid of x altogether:

exp_format <- function() {
exponent <- math_format(format = scientific_format(digits = 2))
trans_format("log10", exponent)
}

qplot(c(1,10),c(1,10)) +
scale_y_log10(labels = exp_format()) +
scale_x_log10(labels = exp_format())

Reply all
Reply to author
Forward
0 new messages