How to avoid overlapping y axis labels between facets

2,622 views
Skip to first unread message

Etienne B. Racine

unread,
Dec 12, 2012, 3:11:07 PM12/12/12
to ggp...@googlegroups.com
How can I avoid overlapping y-axis labels between facets.

e.g. :
qplot(mpg / 100, wt, data = mtcars, facets = vs ~ am)

Generally has overlapping labels on the Y-axis (0.35 meets 0.10). Is there an easy workaround ?

Thanks,
Etienne

Dennis Murphy

unread,
Dec 12, 2012, 3:29:26 PM12/12/12
to Etienne B. Racine, ggp...@googlegroups.com
Hi:

This is controlled by the panel.margin theme element:

qplot(mpg / 100, wt, data = mtcars, facets = vs ~ am) +
theme(panel.margin = unit(1, "cm"))

Adjust the margin to your requirements.

Dennis

Etienne B. Racine

unread,
Dec 12, 2012, 3:54:30 PM12/12/12
to Dennis Murphy, ggplot2
Thanks Dennis. My only reservation is that I need the full usage of the plotting area since I have a lot of facets. Would it be possible to play with the y-axis labeling instead of the margins ?

Etienne


2012/12/12 Dennis Murphy <djm...@gmail.com>

Dennis Murphy

unread,
Dec 12, 2012, 5:05:54 PM12/12/12
to Etienne B. Racine, ggplot2
Hi:

I can think of several potential options:

* reduce the font size on the axis tick labels (see theme(axis.text.y)
and its size element);
* increase the limits on the y-range;
* modify the position of the breaks for the y-scale (see scale_y_continuous()).

There may well be others that are more appropriate to your actual
problem, but since you apparently haven't provided a reproducible
example that closely resembles your actual problem, this is all
speculation...

Dennis

Etienne B. Racine

unread,
Dec 13, 2012, 10:51:53 AM12/13/12
to Dennis Murphy, ggplot2
Thanks a lot Dennis, I've decided to remove the two external ticks, however I couldn't find an explicit option for it, so I manually set the breaks.

Juliet Hannah

unread,
Dec 23, 2012, 11:17:57 AM12/23/12
to Dennis Murphy, ggp...@googlegroups.com
All,

Any suggestions on where I'm going wrong ?

qplot(mpg / 100, wt, data = mtcars, facets = vs ~ am) +
theme(panel.margin = unit(1, "cm"))

Error in theme(panel.margin = unit(1, "cm")) :
could not find function "unit"


> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

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

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

other attached packages:
[1] ggplot2_0.9.3

loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2
grid_2.15.2 gtable_0.1.2 labeling_0.1 MASS_7.3-22
munsell_0.4 plyr_1.8
[10] proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.1
scales_0.2.3 stringr_0.6.1

Brandon Hurr

unread,
Dec 23, 2012, 12:51:01 PM12/23/12
to Juliet Hannah, Dennis Murphy, ggplot2
require(grid)


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

Dennis Murphy

unread,
Dec 23, 2012, 4:21:19 PM12/23/12
to Juliet Hannah, ggp...@googlegroups.com
Hi Juliet:

Load the grid package and try again. This is becoming a ggplot2 FAQ :)

Dennis

Ben Bolker

unread,
Dec 23, 2012, 5:20:32 PM12/23/12
to ggp...@googlegroups.com
On 12-12-23 11:17 AM, Juliet Hannah wrote:
> All,
>
> Any suggestions on where I'm going wrong ?
>
> qplot(mpg / 100, wt, data = mtcars, facets = vs ~ am) +
> theme(panel.margin = unit(1, "cm"))
>
> Error in theme(panel.margin = unit(1, "cm")) :
> could not find function "unit"

library(grid)

Tim Keitt

unread,
Apr 8, 2015, 2:41:36 PM4/8/15
to ggp...@googlegroups.com
Or better grid::unit(1, "cm). (Hi Ben)

THK 
Reply all
Reply to author
Forward
0 new messages