Is it possible to add a title to something like p <- PCAPlot(
I tried things like print(p + ggtitle(
PCAPlot(
In order to capture the plot from PCAPlot as an object, you must set do.return = TRUE; you can then manipulate that object as you would any ggplot object.
I'm trying to do this on VlnPlot and no luck. Any idea why?
I've tried:
p <- VlnPlot(cd138_bm, UAMS_markers, do.return = T)
p + ggtitle("my plot")
and also
p <- VlnPlot(cd138_bm, UAMS_markers, do.return = T)
p$theme$plot.title = element_text("my plot")
p
but it just prints the plot without any new title
Hi Rebecca,
Is UAMS_markers more than one feature? If so, the way that VlnPlot returns plots using cowplot::plot_grid removes the ability to theme or add elements to a plot. To get around this, have VlnPlot return the plot list rather than a combined plot by setting return.plotlist = TRUE, then iterate through that plot list adding titles as you see fit.
plotlist <- VlnPlot(object = cd138_bm, features.plot = UAMS_markers, do.return = TRUE, return.plotlist = TRUE)
for (i in 1:length(x = plotlist)) {
plotlist[[i]] <- plotlist[[i]] + UAMS_titles[i] # Have UAMS_titles be a vector of plot titles
}
cowplot::plot_grid(plotlist = plotlist)
Thank you! That worked perfectly for replacing the title on each individual plot. (once I added "ggtitle()" around the "UAMS_titles[i]"). Is there any way for me to place a title on top of the whole figure? For eg, if I'm plotting four genes, I want each gene's name to be plotted above its individual plot as in the default function, but then I also want to have one big title at the top that says "Expression of Pathway X Genes." I realize now that I would probably specify that through the cowplot::plot_grid function call, not through ggplot, but still not exactly sure how
EDIT: this gets me close, I just need to work on formatting
title <- ggdraw() + draw_label("Pathway X Genes", fontface = 'bold')
cowplot::plot_grid(title = title, plotlist = plotlist)
UPDATE: Got it! Thank you so much for pointing me in the right direction!
plotlist <- VlnPlot(object = cd138_bm, features.plot = UAMS_markers, do.return = TRUE, return.plotlist = TRUE)
p1 <- cowplot::plot_grid(plotlist = plotlist)
title <- ggdraw() + draw_label("Pathway X Genes", fontface = 'bold')
cowplot::plot_grid(title, p1, ncol = 1, rel_heights = c(0.1, 1))
And credit to https://htmlpreview.github.io/?https://github.com/wilkelab/cowplot/blob/master/inst/doc/plot_annotations.html for the cowplot title code
Most helpful comment
UPDATE: Got it! Thank you so much for pointing me in the right direction!
And credit to https://htmlpreview.github.io/?https://github.com/wilkelab/cowplot/blob/master/inst/doc/plot_annotations.html for the cowplot title code