首页 > 其他分享 >Redefine library-自定义函数库

Redefine library-自定义函数库

时间:2024-04-14 16:34:38浏览次数:21  
标签:cutoff Redefine dplyr 函数库 自定义 top marker order

1.jjVolcano_Redefine

library(scRNAtoolVis)
#jjVolcano只有9个颜色,Redefine到我的24个颜色,并与我umap中的分群颜色对应
jjVolcano_Redefine <- function (diffData = NULL, myMarkers = NULL, order.by = c("avg_log2FC"), 
                                log2FC.cutoff = 0.25, pvalue.cutoff = 0.05, adjustP.cutoff = 0.01, 
                                topGeneN = 5, col.type = "updown", back.col = "grey93", pSize = 0.75, 
                                aesCol = c("#0099CC", "#CC3333"), legend.position = c(0.7, 
                                                                                      0.9), base_size = 14, 
                                # tile.col = jjAnno::useMyCol("paired",  n = 9), cluster.order = NULL, polar = FALSE, expand = c(-1, 1), flip = FALSE, ...) #修改
                                tile.col = color_used, cluster.order = NULL, polar = FALSE, expand = c(-1, 1), flip = FALSE, ...)
  
{
  diff.marker <- diffData %>% dplyr::filter(abs(avg_log2FC) >= 
                                              log2FC.cutoff & p_val < pvalue.cutoff)
  diff.marker <- diff.marker %>% dplyr::mutate(type = ifelse(avg_log2FC >= 
                                                               log2FC.cutoff, "sigUp", "sigDown")) %>% dplyr::mutate(type2 = ifelse(p_val_adj < 
                                                                                                                                      adjustP.cutoff, paste("adjust Pvalue < ", adjustP.cutoff, 
                                                                                                                                                            sep = ""), paste("adjust Pvalue >= ", adjustP.cutoff, 
                                                                                                                                                                             sep = "")))
  if (!is.null(cluster.order)) {
    diff.marker$cluster <- factor(diff.marker$cluster, levels = cluster.order)
  }
  back.data <- purrr::map_df(unique(diff.marker$cluster), function(x) {
    tmp <- diff.marker %>% dplyr::filter(cluster == x)
    new.tmp <- data.frame(cluster = x, min = min(tmp$avg_log2FC) - 
                            0.2, max = max(tmp$avg_log2FC) + 0.2)
    return(new.tmp)
  })
  top.marker.tmp <- diff.marker %>% dplyr::group_by(cluster)
  top.marker.max <- top.marker.tmp %>% dplyr::slice_max(n = topGeneN, 
                                                        order_by = get(order.by))
  top.marker.min <- top.marker.tmp %>% dplyr::slice_min(n = topGeneN, 
                                                        order_by = get(order.by))
  top.marker <- rbind(top.marker.max, top.marker.min)
  if (!is.null(myMarkers)) {
    top.marker <- diff.marker %>% dplyr::filter(gene %in% 
                                                  myMarkers)
  }
  else {
    top.marker <- top.marker
  }
  p1 <- ggplot2::ggplot(diff.marker, ggplot2::aes(x = cluster, 
                                                  y = avg_log2FC)) + ggplot2::geom_col(data = back.data, 
                                                                                       ggplot2::aes(x = cluster, y = min), fill = back.col) + 
    ggplot2::geom_col(data = back.data, ggplot2::aes(x = cluster, 
                                                     y = max), fill = back.col)
  if (col.type == "updown") {
    p2 <- p1 + ggplot2::geom_jitter(ggplot2::aes(color = type), 
                                    size = pSize) + ggplot2::scale_color_manual(values = c(sigDown = aesCol[1], 
                                                                                           sigUp = aesCol[2]))
  }
  else if (col.type == "adjustP") {
    p2 <- p1 + ggplot2::geom_jitter(ggplot2::aes(color = type2), 
                                    size = pSize) + ggplot2::scale_color_manual(values = c(aesCol[2], 
                                                                                           aesCol[1]))
  }
  p3 <- p2 + ggplot2::scale_y_continuous(n.breaks = 6) + ggplot2::theme_classic(base_size = base_size) + 
    ggplot2::theme(panel.grid = ggplot2::element_blank(), 
                   legend.position = legend.position, legend.title = ggplot2::element_blank(), 
                   legend.background = ggplot2::element_blank()) + ggplot2::xlab("Clusters") + 
    ggplot2::ylab("Average log2FoldChange") + ggplot2::guides(color = ggplot2::guide_legend(override.aes = list(size = 5)))
  p4 <- p3 + ggplot2::geom_tile(ggplot2::aes(x = cluster, y = 0, 
                                             fill = cluster), color = "black", height = log2FC.cutoff * 
                                  2, alpha = 0.3, show.legend = F) + ggplot2::scale_fill_manual(values = tile.col) + 
    ggrepel::geom_text_repel(data = top.marker, ggplot2::aes(x = cluster, 
                                                             y = avg_log2FC, label = gene), max.overlaps = 50, 
                             ...)
  if (polar == TRUE) {
    p5 <- p4 + geomtextpath::geom_textpath(ggplot2::aes(x = cluster, 
                                                        y = 0, label = cluster)) + ggplot2::scale_y_continuous(n.breaks = 6, 
                                                                                                               expand = ggplot2::expansion(mult = expand)) + ggplot2::theme_void(base_size = base_size) + 
      ggplot2::theme(legend.position = legend.position, 
                     legend.title = ggplot2::element_blank()) + ggplot2::coord_polar(clip = "off", 
                                                                                     theta = "x")
  }
  else {
    if (flip == TRUE) {
      p5 <- p4 + ggplot2::scale_y_continuous(n.breaks = 6) + 
        ggplot2::geom_label(ggplot2::aes(x = cluster, 
                                         y = 0, label = cluster)) + ggplot2::theme(axis.line.y = ggplot2::element_blank(), 
                                                                                   axis.text.y = ggplot2::element_blank(), axis.ticks.y = ggplot2::element_blank()) + 
        ggplot2::coord_flip()
    }
    else {
      p5 <- p4 + ggplot2::scale_y_continuous(n.breaks = 6) + 
        ggplot2::geom_text(ggplot2::aes(x = cluster, 
                                        y = 0, label = cluster)) + ggplot2::theme(axis.line.x = ggplot2::element_blank(), 
                                                                                  axis.text.x = ggplot2::element_blank(), axis.ticks.x = ggplot2::element_blank())
    }
  }
  return(p5)
}

标签:cutoff,Redefine,dplyr,函数库,自定义,top,marker,order
From: https://www.cnblogs.com/corrschi/p/18134293

相关文章

  • 实现自定义注解校验方法参数(AOP+自定义注解+自定义异常+全局异常捕获)
    一、实现目的在编写接口的时候,通常会先对参数进行一次校验,这样业务逻辑代码就略显冗杂,如果可以把校验参数的代码进行统一管理,在方法或者属性上直接添加注解就可以实现参数的校验,就可以提升代码编写的效率。二、实现原理通过自定义注解,注解在入参VO的属性上,设定需要满足的条件,然......
  • Windows 自定义服务(Windows Service)管理
    Windows自定义服务(WindowsService)管理在Windows系统中,你可以使用sc.exe命令或者PowerShell来创建自定义服务。以下是两种方法:使用sc.exe命令:打开命令提示符(以管理员身份运行)。使用sc.exe命令创建服务,语法如下:sccreate<ServiceName>binPath="<PathtoE......
  • Ant - Form 自定义组件 form.getFiledsValue 如何获取值
    import{FC,useState}from'react';importtype{SelectProps}from'antd';import{Select,Space,Flex,Input,Button}from'antd';/***扩展选择器组件,可以通过键盘enter输入一个Option*/constInputSelect:FC<{defaultOptio......
  • C语言10-指针(多级指针、空指针、野指针),自定义数据类型(枚举enum、结构体struct)
    第12章指针pointer12.6多级指针指向指针的指针称为多级指针eg:int*ptr1=&num; int**ptr2=&ptr1; int***ptr3=&ptr2;12.7空指针应用场景:1.暂时不确定指向的指针,可以在定义的时候先赋值为NULL2.有些指针函数,如果内部出现异常无法成功实现功能,可以返回NUL......
  • WPF自定义Window
    前言我们使用WPF开发客户端软件时,一般来讲都不会直接使用默认的Window样式,因为很难符合项目的风格,所以我们一般会自定义Window,一般有两种方式。WindowStyle=None和自定义Window,本文主要介绍第二种。一、WindowStyle=NoneWindowStyle="None"将Window的整个边框就去掉了,好处是......
  • docker network之 自定义网络(重点,多容器时都是使用这个)
    原来的默认使用bridge模式,创建好容器以后,2个容器使用ip地址去ping对方的ip是ok的,但是按照容器的服务名字取ping就失败: 我们知道容器在重启后,ip是可能变化的。所以那总不可能按照ip去访问吧,最好是按照服务名去访问,那怎么处理呢,请看下方:dockernetworklsdockernetworkcrea......
  • SpringBoot starter 原理及如何自定义 starter
     前言项目的开发要求是不断进化的,而随着时间以及技术的推移,在项目中除了基本的编程语言外,还需要进行大量的应用服务整合。例如,在项目中使用MySQL数据库进行持久化存储,同时会利用Redis作为缓存存储,以及使用RocketMQ实现异构系统整合服务等。但在早先使用Spring开发的......
  • 右击canvas画布时,显示自定义菜单
    <style> .clickRightMenu{ width:110px; background-color:#fff; font-size:12px; position:absolute; text-align:left; padding:2px0; border:1pxsolid#ccc; display:none; z-index:100; } .clickRightMe......
  • directive自定义指令把文案转html+输入功能
    文案: "甲方(贷款人):全称:$var<text_aasdrup06rc00>法定代表人/负责人:$var<text_k2s9cffkhls00>地址:$var<text_6356yvx7oag00>\n\n乙方(借款人):全称/姓名:(变量-姓名),身份证号码/统一社会信用代码:(变量-身份证)地址:$var<text_1g9bed61qp7k0>\n\n鉴于:\n\n甲方与乙方于 $var<date_3ld......
  • 自定义校验(这里是Validation)
    1.自定义注解Statepackagecom.itheima.anno;importcom.itheima.Validator.StateValidator;importjakarta.validation.Constraint;importjakarta.validation.Payload;importjava.lang.annotation.*;@Documented//元注解@Target(ElementType.FIELD)@Retention(R......