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

I am developing a custom ggplot theme that includes horizontally rotated Y axis labels, and I want to increase the distance between the label labels and the axis labels. This post suggests setting the parameter vjust, but this is not appropriate in this case. Alignment (for example, left or right in the field) differs from the interval of this field relative to the tick mark.

For example, with axis.title.y=element_text(angle=0, vjust=1, hjust=1)), then I get the correct alignment, but it is too close to the tick marks:

image with hjust = 1

If I installed hjust=2, the text will no longer be correctly cleared correctly:

image with hjust = 2

I played with theme options margin, but I don't think they apply here. Any ideas?

MWE :

df <- data.frame(x=1:10, y=1:10)
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")
+4
2

, , 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)
+3

plot.margin . , hjust -10, plot.margin( , , , ).

, :

()

df <- data.frame(x=1:10, LongAxisLabel=1:10)
ggplot(df, aes(x,LongAxisLabel)) +
  geom_line() +
  theme(axis.title.y=element_text(angle=0, vjust=1, hjust=-1))+
  theme(plot.margin = unit(c(1,1,1,1), "cm"))

Cheers,


. expression(), . . , , , - , . .

library(grid)
df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x,y)) + 
  geom_line() + 
  theme(axis.title.y=element_text(angle=0, vjust=1, hjust=-1), plot.margin=unit(c(3,1,1,3), "cm")) + 
  labs(y=expression("This\nis a\nreally long\naxis\nlabel"))

enter image description here

0

All Articles