[r] Improve trackplot_combine layout logic#116
Conversation
|
From offline suggestion, just added some smarter logic to decide whether to put the color legend above/below the side plot. Earlier plots are unchanged, but this new variant will look better: Additional codeexpression_plot_subset <- ggplot2::ggplot(expression[cell_types %in% c("B", "DC"),], ggplot2::aes(cell_types[cell_types %in% c("B", "DC")], gene, fill=cell_types[cell_types %in% c("B", "DC")])) +
ggplot2::geom_boxplot() +
ggplot2::guides(y="none", fill="none") +
ggplot2::labs(x=NULL, y="RNA") +
ggplot2::scale_fill_manual(values=discrete_palette("stallion"), drop=FALSE) +
BPCells:::trackplot_theme()
coverage_no_sidebar <- coverage_plot
coverage_no_sidebar$trackplot$takes_sideplot <- FALSE
plot3 <- trackplot_combine(
list(
scalebar_plot,
coverage_no_sidebar,
gene_plot,
loop_plot,
coverage_plot_narrow,
gene_plot_narrow,
scalebar_narrow
),
side_plot = expression_plot_subset
)
ggplot2::ggsave(plot=plot3, "pre_trackplot_with_low_side.png", width=8, height=8, units="in") |
| # Reduce cut-off y-axis labels. Put plots with y axis labels later in the plot list, as they will take layer priority with patchwork | ||
| has_y_axis <- vapply(tracks, function(t) is(t, "ggplot") && !is.null(t$labels$y), logical(1)) | ||
| tracks <- c(tracks[!has_y_axis], tracks[has_y_axis]) | ||
| areas <- c(areas[!has_y_axis], areas[has_y_axis]) |
immanuelazn
left a comment
There was a problem hiding this comment.
I played around with the trackplotting code a little bit and it looks really good now. Even fixed all the issues I mentioned on slack, with the cut off coverage labels and legends. Great job Ben
| if (tracks[[i]]$trackplot$keep_vertical_margin) { | ||
| collapse_upper_margin[i] <- FALSE | ||
| if (i < length(tracks)) collapse_upper_margin[i+1] <- FALSE | ||
| } |
There was a problem hiding this comment.
Pragmatically speaking and unrelated, isn't it possible to have this done to group by different regions, and only collapse within region? Would probably remove the dependence on comparing last_region
There was a problem hiding this comment.
I wanted to keep the function so that plots are always shown top-to-bottom in the order given, so I didn't want to do any re-ordering in that way. My aim with this is to do as much collapsing as is possible while to keeping the order the same


Add several improvements to the
trackplot_combinelayout logic.For plots without sideplots, before and after are as follows:
trackplot_gene()color legend to always show both + and - strandtrackplot_scalebar()to change the font size as requested (before it just affected track height)trackplot_coverage()to reduce (though not eliminate) the likelihood of the label getting cut offAnd, when
side_plotis present:Script used for plotting demo (relies on the `pbmc-3k` vignette data)
Before (no sideplot)
After (no sideplot)
Before (with sideplot)
After (with sideplot)
After with a low sideplot (see code from comment below)
(Edited to update plot previews with the later improvements)