Increase distance between text and title on the y-axis

Ernest A picture Ernest A · Jan 23, 2013 · Viewed 124.1k times · Source

The y-axis title appears too close to the axis text.

ggplot(mpg, aes(cty, hwy)) + geom_point()

ggplot output

I have tried changing the value of many parameters with theme() but none seems to help.

Answer

Didzis Elferts picture Didzis Elferts · Jan 23, 2013

From ggplot2 2.0.0 you can use the margin = argument of element_text() to change the distance between the axis title and the numbers. Set the values of the margin on top, right, bottom, and left side of the element.

ggplot(mpg, aes(cty, hwy)) + geom_point()+
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)))

margin can also be used for other element_text elements (see ?theme), such as axis.text.x, axis.text.y and title.