Unexpected interaction of xlim and scale... name=

42 views
Skip to first unread message

Rob James

unread,
Jun 1, 2011, 10:00:06 PM6/1/11
to ggplot2
When xlim() or ylim() is specified, name labels (at least as specified
to scale_continuous) are suppressed.

Consider:
library(data)

ggplot(data=diamonds)+geom_smooth(aes(x=carat, y=price))+
scale_x_continuous(name="X axis") + scale_y_continuous(name="Yaxis")

#(Labels are substituted on axes, as expected)

ggplot(data=diamonds)+geom_smooth(aes(x=carat, y=price))+
scale_x_continuous(name="X axis") + scale_y_continuous(name="Yaxis")+
xlim(1,4)+ylim(300, 16000)
# (Variable names appear on axes, despite specification to scale_continuous


#(Variable names appear on axes)

Is this a feature?

SessionInfo()

sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C
[3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8
[5] LC_MONETARY=C LC_MESSAGES=en_US.utf8
[7] LC_PAPER=en_US.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C

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

other attached packages:
[1] mgcv_1.7-6 ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4
[5] plyr_1.5.2 rms_3.3-0 Hmisc_3.8-3 survival_2.36-9

loaded via a namespace (and not attached):
[1] cluster_1.13.3 digest_0.4.2 lattice_0.19-26
Matrix_0.999375-50
[5] nlme_3.1-101 RColorBrewer_1.0-2 tools_2.13.0
>

Brandon Hurr

unread,
Jun 2, 2011, 2:31:05 AM6/2/11
to Rob James, ggplot2
The order matters...

ggplot(data=diamonds)+geom_smooth(aes(x=carat, y=price))+
    xlim(1,4)+ylim(300, 16000)+scale_x_continuous(name="X axis") + scale_y_continuous(name="Yaxis")

Put the names last and they stick. Put the names in the middle and the limit changes overwrite the variable names you supply. Not sure this is the way it should work, but this is the way that it does. 

Brandon

Kohske Takahashi

unread,
Jun 2, 2011, 4:08:21 AM6/2/11
to Brandon Hurr, Rob James, ggplot2
Hi

Note that if you call scale_x_*() after xlim(), then the xlim() is ignored.
xlim() is just like a syntax suger, and both xlim() and scale_x_*()
return scale object.
So if you want to set both limits and names, set them in the call of scale_x_*()

ggplot(data=diamonds)+geom_smooth(aes(x=carat,
y=price))+scale_x_continuous(name="X axis", xlim=c(1,4))

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Dennis Murphy

unread,
Jun 2, 2011, 8:58:11 AM6/2/11
to Rob James, ggplot2
Hi:

Another thing: if you want *exactly* the x and y limits specified, you
should use coord_cartesian(). Compare the following two
specifications:

ggplot(data=diamonds)+geom_smooth(aes(x=carat, y=price))+
scale_x_continuous(name = 'X axis', xlim = c(1, 4)) +
scale_y_continuous(name = 'Y axis', xlim = c(300, 16000))

ggplot(data=diamonds)+geom_smooth(aes(x=carat, y=price))+
coord_cartesian(xlim = c(1, 4), ylim = c(300, 16000)) +
labs(x = 'X axis', y = 'Y axis')

In the latter case, flipping the order of coord_cartesian() and labs()
makes no difference.

HTH,
Dennis

Rob James

unread,
Jun 2, 2011, 11:51:36 AM6/2/11
to Dennis Murphy, ggplot2
Thanks for the input. Clearly order matters.

Expressed differently, the issue is that xlim and ylim can be called
either as independent functions, or as functions/arguments nested within
scale_ and coord_ function. Only when xlim or ylim are used
independently does this problem arise. Perhaps the standard coding
practice should be to use these functions within calls to scale or coord..

Many thanks,

Rob


Reply all
Reply to author
Forward
0 new messages