mpg$class <- paste(mpg$class, ",long axis label test") p <- ggplot(mpg, aes(class)) + geom_bar() + coord_flip()
place axis ticks in the first column
new.y.label.grob <- gtable_add_cols(new.y.label.grob, widths = y.label.grob[["widths"]][2]) new.y.label.grob <- gtable_add_grob(new.y.label.grob, y.label.grob[["grobs"]][[2]], t = 1, l = 1)
place axis labels in the second column
new.y.label.grob <- gtable_add_cols(new.y.label.grob, widths = y.label.grob[["widths"]][1]) new.y.label.grob <- gtable_add_grob(new.y.label.grob, y.label.grob[["grobs"]][[1]], t = 1, l = 2)
add third column that takes up all the remaining space
new.y.label.grob <- gtable_add_cols(new.y.label.grob, widths = unit(1, "null")) gp <- gtable_add_grob(gp, new.y.label.grob, t = gp$layout$t[which(gp$layout$name == "panel")], l = gp$layout$l[which(gp$layout$name == "panel")]) grid.newpage() grid::grid.draw(gp)
wd <- grid::convertWidth( y.label.grob[["widths"]][1], "cm", valueOnly = TRUE )
margin
p2 + theme(axis.text.y = element_text(margin = margin(0, -wd, 0, 0, "cm")))