Recalculating ticks/labels after using expand on an axis

44 views
Skip to first unread message

Bob

unread,
Jul 21, 2010, 3:31:40 PM7/21/10
to ggplot2
Hi all,

How do I get the ticks and tick labels to recalculate after using
expand to "stretch" the y-axis? I am using facet_wrap with free y
scales, so I can't just use ylim to increase the y-axis limits.

economics$RandFactor <- c("FactorA","FactorB")
p <- ggplot(economics, aes(x=date, y=psavert))
p + geom_point() + scale_y_continuous(expand=c(0.05,0)) + facet_wrap(~
RandFactor, ncol=1, scales="free_y")

If I feel that limiting the plot to the range of the data
"unnecessarily" magnifies fluctuations due to domain knowledge, I can
use the expand function to broaden the y-axis limits. However, the
ticks and tick-labels don't seem to recalculate. To take an extreme
case, let's try it with:

p + geom_point() + scale_y_continuous(expand=c(0.05,0)) + facet_wrap(~
RandFactor, ncol=1, scales="free_y")

I get all the tick labels in the center of the plot. How do I get them
to recalculate, so they span the whole (expanded) y-axis?

Many thanks,
Bob

P.S. I understand from the ggplot2 website that the two numbers are a
multiplicative factor and an additive one - does that mean that the
default expand=c(0.05, 0) means 5% padding of the range on each side?
c(1,0) means doubling of the range on both sides (so it's actually
three times the y-range as the original unmodified plot)? And c(0,1)
means increasing the scale by 1 on each side (in the same units as the
data)?

Dennis Murphy

unread,
Jul 21, 2010, 5:33:57 PM7/21/10
to Bob, ggplot2
Hi:

When I run your example, I get the plot attached to this mail. I don't see any obvious scaling problems. If yours isn't the same, which versions of ggplot2 and plyr do you have? [I don't know if the plot will get through to the list, but it should to the OP.]

> sessionInfo()
R version 2.11.1 (2010-05-31)
x86_64-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                         
[5] LC_TIME=English_United States.1252   

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods 
[8] base    

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8   reshape_0.8.3 plyr_1.0.3  

loaded via a namespace (and not attached):
[1] tools_2.11.1

Dennis


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

econex.png

Bob Chan

unread,
Jul 21, 2010, 5:50:46 PM7/21/10
to Dennis Murphy, ggplot2
Here is my session info. Looks like all the same version numbers.

> sessionInfo()
R version 2.11.1 (2010-05-31)

i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United
Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United
Kingdom.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets

methods base

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.0.3

loaded via a namespace (and not attached):

[1] digest_0.4.2 tools_2.11.1

My mistake - I see the problem. My "extreme" example wasn't different
at all. Try changing 0.05 to 0.5. You should see something like the
plot I've attached.

Bob

econex0.5.png

baptiste auguie

unread,
Jul 22, 2010, 1:34:33 AM7/22/10
to Bob Chan, Dennis Murphy, ggplot2
Hi,

Can you not use expand_limits() for this?

p + geom_point() + facet_wrap(~
RandFactor, ncol=1, scales="free_y") +
scale_y_continuous(expand=c(0,0)) + expand_limits(y=c(-20,20))

HTH,

baptiste

Bob Chan

unread,
Jul 22, 2010, 11:31:45 AM7/22/10
to baptiste auguie, Dennis Murphy, ggplot2
Thanks for the suggestion, Baptiste. Unfortunately, my example was not
complex enough to show why I wanted to use expand(). In my dataset,
the two facets have slightly different ranges, so a fixed ylim won't
work. If I can modify my example as follows:

economics$RandFactor <- c("FactorA","FactorB")

economics$psavert[economics$RandFactor == "FactorB"] <-
economics$psavert[economics$RandFactor == "FactorB"] - 30

p <- ggplot(economics, aes(x=date, y=psavert))

p + geom_point() + facet_wrap(~ RandFactor, ncol=1, scales="free_y")

Now you can see that static expanded limits won't work. I want the
data in each facet to be "centered" vertically, but to expand the
y-limits for each facet so that the minor fluctuations in each facet
are visually minimized.

Hadley Wickham

unread,
Jul 23, 2010, 8:39:31 AM7/23/10
to Bob Chan, baptiste auguie, Dennis Murphy, ggplot2
Hi Bob,

You're using the expand parameter for a purpose it wasn't designed for
- the point of it is to give a little space between the data and the
ticks, so adding ticks in that gap would defeat the purpose. However,
if you look at the source code for expand_limits, you can see
modifying for your needs is pretty simple. You just construct a data
frame that provides both the y values and the facet values and then
use that with geom_blank.

ranges <- data.frame(RandFactor = c("a", "a", "b", "b"), y = c(1, 100,
1000, 10000))
p + geom_blank(aes(y = y), data = ranges, inherit.aes = FALSE)

Hadley

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

Reply all
Reply to author
Forward
0 new messages