Increase the distance between the text and the heading along the y axis

Y axis header too close to axis text.

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

ggplot output

I tried to change the value of many parameters with theme() , but no one helps.

+59
r layout plot ggplot2
Jan 23 '13 at 18:52
source share
2 answers

In ggplot2 2.0.0 you can use the margin = element_text() argument to change the distance between the axis header and the numbers. Set margin values ​​on the t op, r ight, b ottom and l eft 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 .

+97
Jan 23 '13 at 18:57
source share

Based on this forum post: https://groups.google.com/forum/#!topic/ggplot2/mK9DR3dKIBU

It seems that the easiest way is to add a line break (\ n) before your x axis and after your y axis labels. It seems a lot easier (albeit dumb) than the solutions posted above.

 ggplot(mpg, aes(cty, hwy)) + geom_point() + xlab("\nYour_x_Label") + ylab("Your_y_Label\n") 

Hope this helps!

+34
Nov 07 '14 at 9:42 on
source share



All Articles