Label options for scale_[x,y]_log10

54 views
Skip to first unread message

rob...@gmail.com

unread,
Dec 29, 2008, 6:59:15 PM12/29/08
to ggplot2
Take the command
qplot(carat, price, data=diamonds) + scale_x_log10() + scale_y_log10
()

The x and y logarithmic axis labels are transformed to the
"scientific" (10^n) notation. Is there an option to keep the usual
labels there? In fact, the usual labels (not 10^n) seems to be the
most desired notation if fewer than 3 orders of magnitude of numbers
belong to the axis. In financial applications, for instance, a y log
scale for "Value of $1 Invested" is preferred (so that the slope
corresponds to the percent return in any period), but the y axis
rarely goes above 10, which makes the 10^n notation awkward.

Thanks,

Roberto

hadley wickham

unread,
Dec 29, 2008, 7:03:59 PM12/29/08
to ggplot2
Hi Roberto,

Revamping the automatic labelling for log scales is on my to do list,
but until I fix it, you can manually specify the breaks and labels:

+ scale_x_log10(breaks = 10^(1:5), breaks = 10^(1:5))

Hadley
--
http://had.co.nz/

rob...@gmail.com

unread,
Dec 29, 2008, 7:14:26 PM12/29/08
to ggplot2
Hi Hadley,

First of all, congratulations on your great new approach to graphics.

I tried your suggestion, but had problems using it together with
facet_grid(scales="free_y"). For instance, in your example, if the
bottom plot in my facet graph has max(y) = 200, the 10^(3:5) tick
marks spill over the next (2nd to bottom) plot. Is there a way around
this problem?

Thanks,

Roberto

hadley wickham

unread,
Dec 29, 2008, 7:25:35 PM12/29/08
to rob...@gmail.com, ggplot2
Hi Roberto,

I'm glad you're enjoying ggplot2. Unfortunately the free scales are
still pretty raw and there probably quite a few bugs to uncover, like
the one that you've just found. I'll add that to my to do list (if
you could send me a reproducible example that would be really
helpful). In the short term I'm not sure there's anyway you can work
around it on your own.

Hadley
--
http://had.co.nz/

rob...@gmail.com

unread,
Dec 29, 2008, 8:09:38 PM12/29/08
to ggplot2
Hi Hadley,

Here's a hopefully reproducible example:

---

dts <- seq(as.Date("2008-1-1"), as.Date("2008-12-31"), by="day")
set.seed(1)
y1 <- cumprod(1.001 + rnorm(366)/100)
y2 <- cumprod(1.01 + rnorm(366)/100)
da <- data.frame(Date=dts, Liar=y2, Excellent=y1)

require(reshape)
mda <- melt(da, id.vars = "Date")

ytx <- 2^(0:4)
p <- ggplot(mda, aes(Date, value)) +
scale_x_date("") +
scale_y_log10("Value of $1 Investment", breaks = ytx, labels =
ytx)
p + geom_line(aes(colour=factor(variable))) +
opts(title="An Unreal Performance") +
scale_colour_manual("Manager", values=c("red","purple")) +
facet_grid(variable~., scales = "free_y")

-----
The "2" label and tick mark of the bottom plot spills over to the top
plot.

Thanks for looking at it.

Roberto

hadley wickham

unread,
Dec 29, 2008, 9:13:52 PM12/29/08
to rob...@gmail.com, ggplot2
Hi Roberto,

Do you have ggplot2 0.8.1 ? I'm not seeing that problem.

Hadley
--
http://had.co.nz/

rob...@gmail.com

unread,
Dec 29, 2008, 10:23:14 PM12/29/08
to ggplot2
No, I had 0.8. I installed 0.8.1 and indeed and took care of the
problem.

Thanks again, and keep on the great work!

Roberto
Reply all
Reply to author
Forward
0 new messages