Increase Distance Between Text and Title on the Y-Axis

Increase distance between text and title on the y-axis

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.

addition

in order to set the margin for axis titles when the axis has a different position (e.g., with scale_x_...(position = "top"), you'll need a different theme setting - e.g. axis.title.x.top. See https://github.com/tidyverse/ggplot2/issues/4343.

Increasing the space between secondary y axis and its title

You can increase the margin on the right by increasing the second argument of plot.margin

+ theme(plot.margin=unit(c(1,3,1,1), "cm"))

And then increase the vjust argument of axis.title.y.right:

+ theme(axis.title.y.right = element_text(vjust=2 or 3 or 4))

Larger numbers for vjust go further away from the axis. Of course you combine the above two "theme" commands into one.

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

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.

How to increase the distance of an axis title from the plot when using non-default axis positions?

In this case, you must use the arguments axis.title.x.top and axis.title.y.right.

Individually specifying axis.title.x and axis.title.y, which works when the axis titles are in the default (down, left) positions, still doesn't work.

Here is the full example, with correct spacing:

example.data <- data.frame(x.var = rep(-2:2, 5),
y.var = rep(-2:2, each=5),
boolean.var = as.logical(sample(1:1000, 25) %% 2))

library(ggplot2)
library(tidyr)
example.data %>% ggplot(aes(fill = boolean.var)) +
geom_rect(xmin = -1, xmax = 1, ymin = -1, ymax = 1) +
scale_x_continuous(name = "(X Title)", position = "top", limits = c(-0.5,0.5)) +
scale_y_continuous(name = "(Y Title)", position = "right", limits = c(-0.5,0.5)) +
scale_fill_discrete(guide = FALSE) +
facet_grid(y.var ~ x.var) +
theme(panel.margin=unit(0.25 , "lines"),
axis.title = element_text(size = 24, margin = margin(20, 20, 20, 20)),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title.x.top = element_text(margin = margin(1, 0, 15, 0)),
axis.title.y.right = element_text(margin = margin(0, 1, 0, 15)))

Which gives the following output:

Sample Image

How to increase the distance between text and title on *rotated* y-axis in ggplot?

I figured this out eventually, and it requires tweaking the underlying gtable.

library(ggplot2)
library(grid)
library(gtable)
df <- data.frame(x=1:10, y=1:10)
gg <- ggplot(df, aes(x,y)) +
geom_line() +
theme(axis.title.y=element_text(angle=0, vjust=1, hjust=1)) +
labs(y="This\nis a\nreally long\naxis\nlabel")

# Get the table for the ggplot object
g <- ggplotGrob(gg)

# Insert a new column 0.25 inches wide after the second column
# Use gtable_show_layout(g) to figure out which column number to use
g2 <- gtable_add_cols(g, width=unit(0.25, "in"), pos=2)

# Plot the result
grid.draw(g2)

R how to increase spacing between X and Y axis value labels

I've got to agree with user2974951. If you use the below code, you can adjust the width and height values to until there's enough spacing between the tick labels.

my_plot <- ggplot(data = e_2020_Voting_Machines_Per_State) +
geom_point(mapping = aes(x = State_Abbr, y = totalMachines)) + coord_flip()

tiff("my_plot.tiff", width = 8, height =6, units = "cm", res = 300)
print(my_plot)
dev.off()

Or, you can include + element_text(size = 7) to your original ggplot() code to reduce the text size until they're all readable.

How to set space between the axis and the label in matplotlib

You can set bounding by using labelpad argument like this

ax.set_ylabel('Y', rotation=0, labelpad=10)

also you can add space after 'Y ' label in set_ylabel line as following

ax.set_ylabel('Y ',rotation=0)

Note:

As you mentioned you want the same spaces between both axis labels so you can set 'X' label using:

 ax.text(max(x)/2, -(max(y)/10),'X')

and

'Y' label using:

 ax.text(-(max(x)/10), max(y)/2,'Y')

How do I put more space between the axis labels and axis title in an R boxplot

## dummy data
dat <- data.frame(Depth = sample(c(3:6), 20, replace = TRUE), OM = 5 * runif(20))

Add some room for the y-axis labels and annotations, by making the margin bigger on the left hand side of the plot (side = 2):

## margin for side 2 is 7 lines in size
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1

Now plot:

## draw the plot but without annotation
boxplot(OM ~ Depth, data = dat, axes = FALSE, ann = FALSE)
## add axes
axis(1, at = 1:4, labels = c(" ", "Shallow", "Deep", " "), cex.axis = 1.5)
axis(2, cex.axis = 2)
## now draw the y-axis annotation on a different line out from the plot
## using the extra margin space:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
## draw the box to finish off
box()

Then reset the plotting margins:

par(op)

This gives:

boxplot

So we've created more space for the margin of the plot on side 2, and then drawn the axes and the annotation (ylab) separately to control how the plot is spaced out.

So the key to this is this line:

op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1

What we do is save the original graphical parameters in object op, and change the margin sizes (in numbers of lines) to be 5, 7, 4, 2 + 0.1 lines each for the bottom , left, top, right margins respectively. The line above shows the defaults, so the code gives 2 more lines on the left margin than usually provided by default.

Then when we draw the y-axis label using title(), we specify which line (of the 7) to draw the label at:

title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)

Here I used line 4.5, but 5 would work also. The greater the values of 'line' the farther from the plot the label is drawn.

The trick is to find the value for the left margin and the value of 'line' in the title() call that allows the axis tick marks and the axis label to not overlap. Trial and error is likely the best solution to find the values you need with base graphics.



Related Topics



Leave a reply



Submit