17

I am currently using the latest version of ggplot2 from github.

In version 0.8.9 I could do the following to increase space between axis.title and axis.text:

Before:

ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
    axis.title.x = theme_text(vjust=-1.1)
)

Fix:

ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
    axis.title.x = theme_text(vjust=-1.1),
    plot.margin = unit(c(1, 1, 0.8, 0.5), "lines")
)

link to graph

and the axis.title becomes fully visible.

In the latest github version of ggplot2 plot.margin has no effect on axis.title:

ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
    axis.title.x = theme_text(vjust=-0.2),
    plot.margin = unit(c(1, 1, 2, 0.5), "lines"))

link to graph

(notice the increased bottom margin - I can't get plot.background to work in the latest development version)

It seems that 0.8.9 allows axis.title to move over the extra space created by plot.margin, but this is not allowed in the latest development version.

Is there a new way to accomplish this task (or a quick fix for it) in the latest development version?

Any help appreciated.

Community
  • 1
  • 1
KimN
  • 257
  • 1
  • 2
  • 12
  • @BrianDiggs Adding the pictures was much appreciated, however if I edit my post to add additional information I am not allowed to save it unless i revert back to using links. – KimN Feb 03 '12 at 22:13
  • I put the figures back in. If you don't get a response here in reasonable time, try the ggplot2 or ggplot2-dev list, both hosted at google groups. – Brian Diggs Feb 03 '12 at 22:47
  • It would be useful if you could file this as a bug, or mention it on the ggplot2-dev mailing list. – hadley Feb 15 '12 at 18:42
  • @hadley I added a bug report with the quick fix, but linked to here for details - hope that's alright. – KimN Feb 15 '12 at 21:30
  • 1
    This has been changed in the last version, you should use the following instead `theme(axis.title.y=element_text(margin=margin(20,0,0,0)))`, go [here](http://stackoverflow.com/questions/14487188/increase-distance-between-text-and-title-on-the-y-axis) – toto_tico Jul 19 '16 at 17:20

2 Answers2

15

How to do it properly

Use the margin attribute of element_text for axis.title in theme. I use different margins for x and y since I rotate the y title to horizontal (making it easier to read).

library(ggplot2)
library(gridExtra)

ggplot(diamonds, aes(clarity)) +
    geom_bar() +
    theme(
        axis.title.x = element_text(margin = unit(c(3, 0, 0, 0), "mm")),
        axis.title.y = element_text(margin = unit(c(0, 3, 0, 0), "mm"), angle = 0)
    )

Demo of axis title margin

Old hack

opts and theme_text are no longer supported by ggplot. My quick and dirty solution is therefore to just add line breaks at the start or end of the title. It might be ugly but gets the job done with a minimal effort.

ggplot(diamonds, aes(clarity)) +
    geom_bar() +
    xlab("\nClarity") + ylab("Count\n")
Backlin
  • 14,612
  • 2
  • 49
  • 81
  • @branchwarren There's a yet another and more proper way to do it that was missing in both answers. Added it now. – Backlin Aug 19 '16 at 10:24
7

I am closing the question as the fix I made seems to hold for now (see below).

I found the following code in plot-render.r:64:

xlab_height <- grobHeight(xlabel) + 
  if (is.null(labels$x)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_rows(plot_table, xlab_height)
plot_table <- gtable_add_grob(plot_table, xlabel, name = "xlab", 
  l = panel_dim$l, r = panel_dim$r, t = -1)

ylab_width <- grobWidth(ylabel) + 
  if (is.null(labels$y)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_cols(plot_table, ylab_width, pos = 0)
plot_table <- gtable_add_grob(plot_table, ylabel, name = "ylab",
  l = 1, b = panel_dim$b, t = panel_dim$t)

which I changed to:

xlab_height <- grobHeight(xlabel) + 
  if (is.null(labels$x)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_rows(plot_table, xlab_height)
plot_table <- gtable_add_grob(plot_table, xlabel, name = "xlab", 
  l = panel_dim$l, r = panel_dim$r, t = -1, clip = "off") <---- here

ylab_width <- grobWidth(ylabel) + 
  if (is.null(labels$y)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_cols(plot_table, ylab_width, pos = 0)
plot_table <- gtable_add_grob(plot_table, ylabel, name = "ylab",
  l = 1, b = panel_dim$b, t = panel_dim$t, clip = "off") <---- here

This allows one to use the hjust/vjust combined with plot.margin to move the axis.titles without clipping.

On request I filled this a bug on github.

KimN
  • 257
  • 1
  • 2
  • 12