Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206235

Plot size won't adjust when saving ggplot2 objects

$
0
0

I have been trying to save ggplot2 objects using a loop, but for some reason the size of the plots will not adjust. I have tried using ggsave(), and I have tried using 'save_plot', since the objects are actually being created by a Seurat function that uses cowplot. In both saving methods I have tried adjusting width and height (or in the case of save_plot, base_width and base_height), but nothing changes in my plots. My complete code is as follows:

    feature.maker.2 <- function(object, directory = "/Users/montgomerywf/Desktop/Brain-Immune-Cell-Project/Cell Marker Master List (sheet arrangement).xlsx",
                        dstor, bring.to.front = FALSE, folder.name = "FeaturePlots", t.cells = FALSE, ilcs = FALSE, b.cells = FALSE, 
                        myeloid = FALSE, dividing = FALSE) {
                          library(Seurat)
                          library(cowplot)
                          library(readxl)
                          library(ggplot2)
                          library(dplyr)
                          setwd(dstor)
                          dir.create(path = folder.name)
                          setwd(paste(dstor, folder.name, sep = "/"))
                          tcell.sheets <- c(1:10)
                          ilc.sheets <- c(11:17)
                          b.sheets <- c(18:21)
                          myeloid.sheets <- c(22:27)
                          dividing.sheets <- 28
                          sheet.names <- c("th1", "th2", "th17", "tfh", "treg", "t_gammadelta", "t_naive", "t_memory", "cd8_effector", 
                                           "cd8_centralmemory", "ilc1", "ilc2", "ilc3", "nk_regulatory", "nk_tolerant", "nk_cytotoxic",
                                           "nkt", "immature_b", "maturing_b", "mature_b", "presenting_b", "homeostatic_microglia", 
                                           "DAM", "macrophages", "monocytes", "neutrophils", "dendritics", "dividing")
                          graph.maker <- function(sheets) {
                            for (i in sheets) {
                              plot.name <- paste(sheet.names[i], ".png", sep = "")
                              geneset <- read_excel(path = directory, sheet = i, col_names = FALSE)
                              colnames(geneset) <- NULL
                              geneset <- geneset[, 1, drop = TRUE]
                              geneset.plot <- FeaturePlot(object, features = dput(geneset), min.cutoff = 0, sort.cell = bring.to.front)
                              plot_grid(geneset.plot)
                              if (length(geneset) == 2) {
                                save_plot(filename = plot.name, plot = geneset.plot, base_height = 5, base_width = 10)
                              }
                              if (length(geneset) == 3|4) {
                                ggsave(filename = plot.name, height = 10, width = 12, units = "in")
                              }
                              if (length(geneset) == 5|6) {
                                ggsave(filename = plot.name, height = 20, width = 14, units = "in")
                              }
                              if (length(geneset) >= 7 && length(geneset) <= 9) {
                                ggsave(filename = plot.name, height = 20, width = 20, units = "in")
                              }
                              if (length(geneset) >= 10 && length(geneset) <= 12) {
                                ggsave(filename = plot.name, height = 18, width = 24, units = "in")
                              }
                              if (length(geneset) >= 13) {
                                ggsave(filename = plot.name, height = 24, width = 24, units = "in")
                              }
                            }
                          }
                          if (isTRUE(t.cells)) {
                            graph.maker(sheets = tcell.sheets)
                          }
                          if (isTRUE(ilcs)) {
                            graph.maker(sheets = ilc.sheets)
                          }
                          if (isTRUE(b.cells)) {
                            graph.maker(sheets = b.sheets)
                          }
                          if (isTRUE(myeloid)) {
                            graph.maker(sheets = myeloid.sheets)
                          }
                          if (isTRUE(dividing)) {
                            graph.maker(sheets = dividing.sheets)
                          }
                        }

Does anyone know why I can't save my plot in the desired size? I have left the save_plot function in for the length(geneset) == 2 part of the loop, just to show you what it might look like in the rest of the code.


Viewing all articles
Browse latest Browse all 206235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>