r - Adjusting the "margin" space for axis text in ggplot2 -
which property, if any, in ggplot
controls
width (or amount of blank space) of axis text?
in example below, ultimate goal "push in" left-hand side of top graph lines bottom graph.
i tried theme(plot.margin=..)
affects margin of entire plot.
facet
'ing doesn't either, since scales on y different.
as last resort, realize modify axis text itself, need calculate cuts 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"))
here 1 solution.
the idea borrowed "having horizontal instead of vertical labels on 2x1 facets , splitting y-label define 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 plots align_plots1(short, long)
here output.
Comments
Post a Comment