Commit d9a5bb7b authored by HaojiaWu's avatar HaojiaWu
Browse files

fix colors

parent 12a606e4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ plot_cell_fraction<-function(
 
 if(show_replicate){
   p <- p +   geom_quasirandom(size=1,width = 0.2, color='midnightblue', alpha=0.8, groupOnX = F)+facet_wrap(~celltype, scales = 'free_y', ncol = length(celltypes))
   g <- change_strip_background(p, type = 'top', n.color = length(celltypes), strip.color = strip.color)
   g <- change_strip_background(p, type = 'top', strip.color = strip.color)
   print(grid.draw(g))
 } else {
   p
+2 −2
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ complex_dotplot_single <- function(
  }
  if(!is.null(splitby)){
    p <- p +facet_wrap(~splitby, scales = 'free_x')
    g <- change_strip_background(p, type = 'top', n.color = length(levels(seu_obj)), strip.color = strip.color)
    g <- change_strip_background(p, type = 'top', strip.color = strip.color)
    print(grid.draw(g))
  } else {
    p
@@ -172,7 +172,7 @@ complex_dotplot_multiple <- function(
    ylab("")+xlab("")+ggtitle('')+
    facet_wrap(~celltype, ncol = length(levels(seu_obj)))
  )
  g <- change_strip_background(p, type = 'top', n.color = length(levels(seu_obj)), strip.color = strip.color)
  g <- change_strip_background(p, type = 'top',  strip.color = strip.color)
  print(grid.draw(g))
}

+6 −25
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#' @param font.size Font size for the labels.
#' @param pt.size Point size for the data points on the violin
#' @param splitby Group to split the gene expression. Only works when length(groups)==1.
#' @param strip.color Colors for the strip background
#' @return A ggplot object
#' @export

@@ -26,7 +27,8 @@ complex_vlnplot_single <- function(
  add.dot = T,
  font.size=14,
  pt.size=0.1,
  splitby=NULL
  splitby=NULL,
  strip.color=NULL
){
  if(length(feature)>1){
    stop("Only one gene is allowed in this method. Please use complex_vlnplot_multiple if you want to plot multiple genes.")
@@ -67,18 +69,7 @@ complex_vlnplot_single <- function(
      }
      if(!is.null(splitby)){
        p = p + facet_wrap(as.formula(paste("~", splitby)), scales = 'free_x')
        g <- ggplot_gtable(ggplot_build(p))
        strip_t <- which(grepl('strip-t', g$layout$name))
        strip_r <- which(grepl('strip-r', g$layout$name))
        strip_both<-c( strip_r,strip_t)
        ncol <- length(celltypes) + length(names(table(gene_count[,splitby])))
        fills <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(12, "Set3"))(ncol)
        k <- 1
        for (i in strip_both) {
          j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
          g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
          k <- k+1
        }
        g <- change_strip_background(p, type = 'top',  strip.color = strip.color)
        print(grid::grid.draw(g))
      } else {
        p
@@ -108,6 +99,7 @@ complex_vlnplot_single <- function(
        p<- patchwork::wrap_plots(plotlist = plot_list, ncol = 1)  + patchwork::plot_annotation(title = feature) & theme(plot.title = element_text(hjust = 0.5, size = (font.size +2)))
        p
      } else {
        gene_count$celltype<-factor(gene_count$celltype, levels = celltypes)
        p<-ggplot(gene_count, aes_string(x = groups, y = feature, fill = groups)) +
          facet_grid(as.formula(paste("celltype","~", splitby)), scales = "free_x") +
          geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
@@ -123,18 +115,7 @@ complex_vlnplot_single <- function(
        if(add.dot){
          p = p + geom_quasirandom(size=pt.size, alpha=0.2)
        }
        g <- ggplot_gtable(ggplot_build(p))
        strip_t <- which(grepl('strip-t', g$layout$name))
        strip_r <- which(grepl('strip-r', g$layout$name))
        strip_both<-c(strip_t, strip_r)
        ncol <- length(celltypes) + length(names(table(gene_count[,splitby])))
        fills <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(12, "Set3"))(ncol)
        k <- 1
        for (i in strip_both) {
          j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
          g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
          k <- k+1
        }
        g <- change_strip_background(p, type = 'both',  strip.color = strip.color)
        print(grid::grid.draw(g))
      }
    }
+9 −6
Original line number Diff line number Diff line
@@ -385,27 +385,30 @@ firstup <- function(

#' A function to change the strip background color in ggplot
#' @param ggplt_obj A ggplot object
#' @param n.color Number of colors.
#' @param type Strip on the "top" side only or "both" sides
#' @param strip.color A color vector
#' @export
#' 
change_strip_background <- function(
  ggplt_obj, 
  type = "top",
  n.color, 
  strip.color=NULL
  ){
  g <- ggplot_gtable(ggplot_build(ggplt_obj))
  if(type == "top"){
    strip_both <- which(grepl('strip-t', g$layout$name))
    fills<-strip.color
    if(is.null(fills)){
    fills<- scales::hue_pal(l=90)(length(strip_both))
    }
  } else {
    strip_t <- which(grepl('strip-t', g$layout$name))
    strip_r <- which(grepl('strip-r', g$layout$name))
    strip_both<-c(strip_t, strip_r)
  }
    fills<-strip.color
  if(is.null(strip.color)){
    fills<- scales::hue_pal()(n.color)
    if(is.null(fills)){
      fills<- c(scales::hue_pal(l=90)(length(strip_t)),scales::hue_pal(l=90)(length(strip_r)))
    }
  }
  k <- 1
  for (i in strip_both) {
+592 B (170 KiB)
Loading image diff...
Loading