scientific notation: 10^-8 instead of 10e-08

740 views
Skip to first unread message

Johannes Radinger

unread,
May 8, 2012, 7:01:04 AM5/8/12
to ggp...@googlegroups.com
Hi,

I plot I am creating has got a log-scaled axis. Now I'd like to
create manual breaks and add labels to the axis with following command:


scale_x_continuous("Log Axis", breaks=c(0.00000001,0.00001,0.001,0.1,1), labels=c(0.00000001,0.00001,0.001,0.1,1),trans="log")

Now my labels are plotted as 10e-08 instead of the real number. I can set the limit for the scientific notion with options(scipen=10). Then I get the numbers as I defined them in labels=c(...).

But is there a way to set the formatting of such numbers so that all
numbers are displayed as 10^-8 etc in the plot. I guess that this saves space on the axis as it is probably smaller than 10e-08 (6 digits) and of course smaller than 0.00000001.

Any suggestions are highly appreciated...

Best regards,

/johannes

--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

Dennis Murphy

unread,
May 8, 2012, 8:01:19 AM5/8/12
to Johannes Radinger, ggp...@googlegroups.com
Hi:

Are you thinking about something like this?

library('ggplot2')
library('scales')
DF <- data.frame(x = runif(1000, 0.00000001, 1),
y = rnorm(1000))
ggplot(DF, aes(log(x), y)) + geom_point() +
scale_x_continuous(breaks = (-6):(-1),
labels = math_format(10^.x)) +
xlab('x')

math_format() is part of the scales package.

HTH,
Dennis
> --
> 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

Johannes Radinger

unread,
May 8, 2012, 9:24:35 AM5/8/12
to Dennis Murphy, ggp...@googlegroups.com
Hi,

I just tried your example...
..I am not quite sure but I think there is a problem
with your example data and the axis. This has to do with
the log scale.

E.g. check your data you created with runif...
most of them are close to 1 and small values
are very very small values are very rare:

Trying your example with:
set.seed(10)
DF <- data.frame(x = runif(1000, 0.00000001, 1),
y = rnorm(1000))
range(DF$x)


For producing really small numbers one could use:

set.seed(10)
DF <- data.frame(x = exp(runif(1000, log(0.000001), log(1))),
y = rnorm(1000))
range(DF$x)


Now, plotting either of the two DFs reveals that
the axis does not explain the real extent
of the values. Resp. the range of range(DF$x) and the
range of the axis do not match (of course because the were
transformed before)...

p1 <- ggplot(DF, aes(log(x), y)) + geom_point() +
scale_x_continuous(breaks = (-6):(-1),labels = math_format(10^.x))

p2 <- ggplot(DF, aes(x, y)) + geom_point() +
scale_x_continuous(breaks = c(0.000001,0.0001,0.01,0.1),labels=math_format(10^.x),trans="log")

So far as I understand ggplot2, one need
to supply the untransformed data and set trans="log,
to get log scaled axis (as in my example before).
Now there is just the question how to get the labels
like I want them...
I'll look that up into the scales package...
Anyway thank you.... :)


/johannes




-------- Original-Nachricht --------
> Datum: Tue, 8 May 2012 05:01:19 -0700
> Von: Dennis Murphy <djm...@gmail.com>
> An: Johannes Radinger <JRad...@gmx.at>
> CC: ggp...@googlegroups.com
> Betreff: Re: scientific notation: 10^-8 instead of 10e-08
Reply all
Reply to author
Forward
0 new messages