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")
)
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"))
(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.
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.
If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.
To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().
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 .
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)
)
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With