Am I correct that it is currently not possible to change the height of indiviudal facets in a 'multi column' plot? Many thanks!
I am trying to create a facetted heatmap and facets have different numbers of items on the y axis. What I want is that each heatmap's height is proportional to the number of items on the y axis (in the examples below number of different cars).
EDIT: To be more precise, I am not trying to change the hight of the facets but the height of the rectangles (bin-width?)
I am aware that the missing 'space' argument in facet_wrap
was (partly) addressed in the wonderful ggforce
's facet_row/col
, but as far as I understand they 'only' refer to one-dimensional facets.
The plot with facet_wrap
below should have facets of different hight. As its stands, I think, it is visually misleading/distorting.
library(tidyverse)
library(ggforce)
library(patchwork)
mtcars %>%
rownames_to_column(., "car_name") %>%
ggplot()+
geom_tile(aes(y=car_name,
x=gear,
fill=mpg))+
facet_wrap(vars(carb),
nrow=2,
scale="free_y")
Below, facet_grid
offers the space argument. But afaik there is no option to adjust the number of columns (other than including another column).
mtcars %>%
rownames_to_column(., "car_name") %>%
ggplot()+
geom_tile(aes(y=car_name,
x=gear,
fill=mpg))+
facet_grid(vars(carb),
space="free",
scale="free_y")
Here ggforce's facet_col
. Again, afaik there is no ncol option.
mtcars %>%
rownames_to_column(., "car_name") %>%
ggplot()+
geom_tile(aes(y=car_name,
x=gear,
fill=mpg))+
ggforce::facet_col(vars(carb),
space="free",
scale="free_y")
Finally, I gave patchwork
a try and assign the different car numbers per facet as the height argument.
However, once I arrange the plots in multiple columns the facets have the height of the highest facet in the row.
plots_mtcars <- mtcars %>%
rownames_to_column(., "car_name") %>%
group_split(carb) %>%
map(~ggplot(.) +
geom_tile(aes(y=car_name,
x=gear,
fill=mpg))+
labs(title=paste0("carb:", unique(.$carb)))+
theme(legend.position="none"))
plot_heights<- mtcars %>%
group_by(carb) %>%
summarise(n_obs=n()) %>%
pull(n_obs)
wrap_plots(plots_mtcars) +
plot_layout(heights=plot_heights, ncol=2)
Created on 2019-12-06 by the reprex package (v0.3.0)