Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase space between axis.title and axis.text in ggplot2 (version >= 0.9.0)

Tags:

r

ggplot2

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.

like image 699
KimN Avatar asked Feb 03 '12 16:02

KimN


People also ask

How do I change the axis title size in ggplot2?

You can change axis text and label size with arguments axis. text= and axis. title= in function theme() . If you need, for example, change only x axis title size, then use axis.

How do I increase axis label size?

If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.

How do you stop axis labels overlapping in Ggplot?

To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().

How do I change the axis titles in R plots?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


1 Answers

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")
like image 112
Backlin Avatar answered Oct 10 '22 19:10

Backlin