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.
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"))
Increase margins In order to modify the plot margins set the margin function inside the plot. margin component of the theme function. The margins are measured with points ( "pt" ), but you can use other unit measure in the unit argument, like centimeters.
To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element.
Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.
You can change axis text and label size with arguments axis. text= and axis. title= in function theme() . If you need, for example, change only x axis title size, then use axis.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With