1

As a followup to the question of how to Increase distance between text and title on the y-axis, I am wondering whether it is also possible to adjust the distance between an axis and its title/label. This could be useful for creating similarly styled graphs, for example when the tick marks of two graphs are of different magnitudes.

Here are two example plots:

plot_a <- ggplot(mpg, aes(cty, hwy)) + geom_point()
plot_b <- ggplot(mpg, aes(cty, hwy*100)) + geom_point()

Note the difference on the left side of the plots:

enter image description here

enter image description here

Cowplot can, somehow, do the job and set the distance of axis and label the same in two plots:

cowplot::plot_grid (plotlist = list (plot_a, plot_b),
                align = "v",
                nrow = 2,
                ncol = 1,
                greedy = T)

enter image description here

However, I'd rather like to adjust the distance bewteen the label and the axis manually. Also, there are cases where it's preferable to draw one single plot per panel, rendering the cowplot-solution useless. So - is there a possibility to manually adjust the distance between axis and label?

yenats
  • 531
  • 1
  • 3
  • 16
  • which package does `opts` originate from? It just gives me `Error in opts(axis.title.y = theme_text(vjust = -0.5)) : could not find function "opts"` – yenats Nov 30 '19 at 23:39
  • sorry, it is changed to `theme` – akrun Nov 30 '19 at 23:40
  • Now I get `Error in theme_text(vjust = -0.5) : could not find function "theme_text"` - I suppose this might have changed, also? When I use `element_text`, I face the same problem as with @Didzis Elferts proposal to use `margin`: it is not consistent among plots with differing axis text length. – yenats Nov 30 '19 at 23:43
  • 1
    Have you considered using patchwork to create a range of aligned plots? See: https://github.com/thomasp85/patchwork. You could simply cheat by appending "\n " to axis titles, prepending " \n" to x-axis text and appending "\t" to y-axis text. – teunbrand Dec 01 '19 at 08:08
  • patchwork (of which I had never heard before) is, indeed, very interesting. However, my goal is not to combine separate plots into the same graphic. I rather want to produce *separate* plots with identical properties. Like "figure 1", which could be a barchart and "figure 2" which could be a scatterplot. In a final document, both figures should appear on different pages, however, I'd like them to have exactly the same appereance. With the 'cowplot' example I just wanted to show that it somehow must be possible to adjust the distance betwee axis and label on different plots in the same manner. – yenats Dec 02 '19 at 10:24
  • I could also put it the other way round: is there a reproducible way to get the result of the 'cowplot'-example as two *separate* graphics? – yenats Dec 02 '19 at 10:27

0 Answers0