, , - y. , .
y .
library(ggplot2)
library(gridExtra)
library(stringr)
plots = lapply(levels(iris$Species),
function(spec){
ggplot(iris[iris$Species == spec, ], aes(Petal.Length)) +
geom_histogram() +
ggtitle(spec)
})
grid.arrange(grobs = plots, nrow = 1, ncol = 3, top = "Base\n\n")
![base_plots](https://fooobar.com/undefined)
.
,
alignLimits = function(plotsList){
y.limits = sapply(plotsList, function(.){layer_scales(.)$y$range$range})
y.min = min(y.limits[1,])
y.max = max(y.limits[2,])
return(lapply(plotsList,
function(.){. + coord_cartesian(ylim=c(y.min, y.max))}))
}
plots.limits = alignLimits(plots)
grid.arrange(grobs = plots.limits, nrow = 1, ncol = 3, top = "Aligned limits\n\n")
![limits](https://fooobar.com/undefined)
.
, , :
alignLables = function(plotsList){
y.labels = lapply(plotsList, function(.){ggplot_build(.)$layout$panel_ranges[[1]]$y.labels})
maxChars = sapply(y.labels, function(.){max(nchar(.))})
format.labels = function(label){str_pad(label, max(maxChars), pad = " ")}
return(lapply(plotsList, function(.){return(. + scale_y_continuous(labels = format.labels))}))
}
plots.labels = alignLables(plots)
grid.arrange(grobs = plots.labels, nrow = 1, ncol = 3, top = "Aligned labels\n\n")
![labels](https://fooobar.com/undefined)
, - .