Adjusting the "margin" space for axis text in ggplot2

Which property, if any, in ggplot controls the width (or amount of blank space) of the axis text?


In the example below, my ultimate goal is to "push in" the left-hand side of the top graph so that it lines up with the bottom graph.

I tried theme(plot.margin=..) but that affects the margin of the entire plot. facet'ing doesn't help either, since the scales on the y are different.

As a last resort, I realize I could modify the axis text itself, but then I would also need to calculate the cuts for each graph.

Reproducible Example:
library(ggplot2)
library(scales)

D <- data.frame(x=LETTERS[1:5],  y1=1:5, y2=1:5 * 10^6)

P.base <- ggplot(data=D, aes(x=x)) + 
            scale_y_continuous(labels=comma)

Plots <- list(
    short = P.base + geom_bar(aes(y=y1), stat="identity", width=.5)
  , long  = P.base + geom_bar(aes(y=y2), stat="identity", width=.5) 
  )

do.call(grid.arrange, c(Plots, ncol=1, main="Sample Plots"))

Answers


Here is one solution.

The idea was borrowed from "Having horizontal instead of vertical labels on 2x1 facets and splitting y-label Define a function

align_plots1 <- function (...) {
    pl <- list(...)
    stopifnot(do.call(all, lapply(pl, inherits, "gg")))
    gl <- lapply(pl, ggplotGrob)
    bind2 <- function(x, y) gtable:::rbind_gtable(x, y, "first")
    combined <- Reduce(bind2, gl[-1], gl[[1]])
    wl <- lapply(gl, "[[", "widths")
    combined$widths <- do.call(grid::unit.pmax, wl)
    grid::grid.newpage()
    grid::grid.draw(combined)
}

short <- P.base + geom_bar(aes(y=y1), stat="identity", width=.5)
long <- P.base + geom_bar(aes(y=y2), stat="identity", width=.5) 

#Now, align the plots
align_plots1(short, long)

Here is the output.


Need Your Help

Redis on Heroku saving multiple hash keys as one. But OK on local redis db

node.js heroku redis

We are having an issue with heroku-redis where new hash keys are being saved in the same value

Best method for showing/hiding form elements based on other form input and validation?

c# jquery asp.net

I am in the process of revamping a website using ASP.NET C# web forms. I have very little experience on the matter, and wondered. Is there best practice for showing or hiding form elements based on...