axis labels in 10^x format in ggplot 0.9.2.1

8,396 views
Skip to first unread message

Roey Angel

unread,
Oct 17, 2012, 6:54:42 AM10/17/12
to ggp...@googlegroups.com
Previous to ggplot 0.9 I used to use this function to convert number formats on axis labels to scientific 10^x format (10'superscript'x)
myfmt <- function(x, ...)
   format(x, big.mark = '', trim = TRUE, scientific = TRUE, ...)


and then used the `formatter` option in scale_continuous to call it
scale_y_continuous(formatter = myfmt)

As of V0.9 formatter has been disabled in the new scales package.

Scales allows producing labels of the type `1ex` using:
scale_y_continuous(labels = scientific_format())
But no option produces the 10^x format

And ggplot2_0.9.0_changes describes how to get it for a log transformed axis
scale_y_log10old <- function(...) {
scale_y_log10(breaks = trans_breaks('log10', function(x) 10^x),
labels = trans_format('log10', math_format(10^.x)), ...)
}

But I couldn't figure out how to get it to work on a continuous axis, i.e. simply display '1000' as '10^3' without transforming anything.

This is most likely been answered somewhere already but I just couldn't find it anywhere.

Roey

Brandon Hurr

unread,
Oct 17, 2012, 8:56:38 AM10/17/12
to Roey Angel, ggp...@googlegroups.com
Something like this?

require(ggplot2)
require(scales)

df<-data.frame(x=c(1.1, 10, 100, 1000, 10000), y=c(1,2,3,4,5))
ggplot(data=df, aes(x=x, y=y))+scale_x_log10(labels=trans_format('log10',math_format(10^.x)))+geom_point()




Roey

--
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

Roey Angel

unread,
Oct 17, 2012, 9:32:05 AM10/17/12
to ggp...@googlegroups.com, Roey Angel
Thanks Brandon,
But actually that's what I was trying to avoid.
Your code transforms the x axis from linear to logarithmic where's what I wanted is simply to change the number format.
I'd like to get the plot one gets from:
df<-data.frame(x=c(1.1, 10, 100, 1000, 10000), y=c(1,2,3,4,5))
ggplot(data=df, aes(x=x, y=y))+geom_point()

but have the values on the x-axis displayed as '2.5 10^3', '5.0 10^3', '7.5 10^3' etc.
Roey

Brandon Hurr

unread,
Oct 17, 2012, 9:39:30 AM10/17/12
to Roey Angel, ggp...@googlegroups.com
How about this?

ggplot(data=df, aes(x=x, y=y))+scale_x_continuous(labels=trans_format('log10',math_format(10^.x)))+geom_point()

Roey Angel

unread,
Oct 17, 2012, 9:45:45 AM10/17/12
to ggp...@googlegroups.com, Roey Angel
I tried this already and the result is:
10^-Inf 10^3.39794 10^3.69897 ...
which are all correct (i.e. 10^3.39794 is very close to 2500) but are not a helpful format.

Brandon Hurr

unread,
Oct 17, 2012, 9:48:31 AM10/17/12
to Roey Angel, ggp...@googlegroups.com
Can you tell I don't usually mess with scales? 

You might have to make your own trans formatter again... 

?scales::trans_new

Brian Diggs

unread,
Oct 18, 2012, 2:29:36 PM10/18/12
to Roey Angel, ggplot2
On 10/17/2012 6:48 AM, Brandon Hurr wrote:
> Can you tell I don't usually mess with scales?
>
> You might have to make your own trans formatter again...
>
> ?scales::trans_new

It is "simpler" than that. You don't need to create a transformation,
just a function that formats the labels (as already determined) into
what is desired. That is, you just need a labels function. This one will
work:

fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}

Which you can then use as

ggplot(data=df, aes(x=x, y=y)) +
geom_point() +
scale_x_continuous(labels=fancy_scientific)

This may not be exactly what you want; it displays 0 as 0.0 x 10^+0 and
1000 as 1.0 x 10^+4. If you want something different, then you have to
pull the relevant parts out from a vector of positions and assemble the
expressions yourself.

> On Wed, Oct 17, 2012 at 2:45 PM, Roey Angel <angel-QidqKpz2+uDjt...@public.gmane.org>wrote:
>
>> I tried this already and the result is:
>> 10^-Inf 10^3.39794 10^3.69897 ...
>> which are all correct (i.e. 10^3.39794 is very close to 2500) but are not
>> a helpful format.
>>
>>
>>
>> On Wednesday, October 17, 2012 3:39:51 PM UTC+2, Brandon Hurr wrote:
>>
>>> How about this?
>>>
>>> ggplot(data=df, aes(x=x, y=y))+scale_x_continuous(**
>>> labels=trans_format('log10',**math_format(10^.x)))+geom_**point()
>>>
>>>
>>>
>>> On Wed, Oct 17, 2012 at 2:32 PM, Roey Angel <an...-QidqKpz2+uDjtQW52tizGYQuADTiUCJX@public.gmane.org>wrote:
>>>
>>>> df<-data.frame(x=c(1.1, 10, 100, 1000, 10000), y=c(1,2,3,4,5))
>>>> ggplot(data=df, aes(x=x, y=y))+scale_x_log10(labels=**tra**
>>>> ns_format('log10',math_**format(**10^.x)))+geom_point()
>>>>
>>>
>>> --
>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org
>> More options: http://groups.google.com/group/ggplot2
>>
>


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

Roey Angel

unread,
Oct 18, 2012, 2:59:04 PM10/18/12
to ggp...@googlegroups.com, Roey Angel
Thanks Brian,
The function works great and I can take it from here, i.e. I know my way around regular expressions just never thought of using them for this purpose.
> On Wed, Oct 17, 2012 at 2:45 PM, Roey Angel <angel-QidqKpz2+uDjtQW52tizGYQuADTiUCJX@public.gmane.org>wrote:
>
>> I tried this already and the result is:
>> 10^-Inf 10^3.39794 10^3.69897 ...
>> which are all correct (i.e. 10^3.39794 is very close to 2500) but are not
>> a helpful format.
>>
>>
>>
>> On Wednesday, October 17, 2012 3:39:51 PM UTC+2, Brandon Hurr wrote:
>>
>>> How about this?
>>>
>>>   ggplot(data=df, aes(x=x, y=y))+scale_x_continuous(**
>>> labels=trans_format('log10',**math_format(10^.x)))+geom_**point()
>>>
>>>
>>>
>>> On Wed, Oct 17, 2012 at 2:32 PM, Roey Angel <an...-QidqKpz2+uDjtQW52tizGYQuADTiUCJX@public.gmane.org>wrote:
>>>
>>>> df<-data.frame(x=c(1.1, 10, 100, 1000, 10000), y=c(1,2,3,4,5))
>>>> ggplot(data=df, aes(x=x, y=y))+scale_x_log10(labels=**tra**
>>>> ns_format('log10',math_**format(**10^.x)))+geom_point()
>>>>
>>>
>>>   --
>> 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 ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3M...@public.gmane.org

elm...@statlab.bio5.org

unread,
Mar 16, 2017, 1:08:50 PM3/16/17
to ggplot2, angel-QidqKpz2+uDjt...@public.gmane.org
Thank you Brian. I was looking for a solution for this problem and you saved my time!
> On Wed, Oct 17, 2012 at 2:45 PM, Roey Angel <angel-QidqKpz2+uDjtQW52tizGYQuADTiUCJX@public.gmane.org>wrote:
>
>> I tried this already and the result is:
>> 10^-Inf 10^3.39794 10^3.69897 ...
>> which are all correct (i.e. 10^3.39794 is very close to 2500) but are not
>> a helpful format.
>>
>>
>>
>> On Wednesday, October 17, 2012 3:39:51 PM UTC+2, Brandon Hurr wrote:
>>
>>> How about this?
>>>
>>>   ggplot(data=df, aes(x=x, y=y))+scale_x_continuous(**
>>> labels=trans_format('log10',**math_format(10^.x)))+geom_**point()
>>>
>>>
>>>
>>> On Wed, Oct 17, 2012 at 2:32 PM, Roey Angel <an...-QidqKpz2+uDjtQW52tizGYQuADTiUCJX@public.gmane.org>wrote:
>>>
>>>> df<-data.frame(x=c(1.1, 10, 100, 1000, 10000), y=c(1,2,3,4,5))
>>>> ggplot(data=df, aes(x=x, y=y))+scale_x_log10(labels=**tra**
>>>> ns_format('log10',math_**format(**10^.x)))+geom_point()
>>>>
>>>
>>>   --
>> 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 ggplot2-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3M...@public.gmane.org
>> More options: http://groups.google.com/group/ggplot2
>>
>


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


Reply all
Reply to author
Forward
0 new messages