• 记录R的一些黑魔法


    通路富集结果可视化

    1
    2
    3
    4
    5
    6
    7
    8
    pathway<-read.table("PTC+_transcript_pep_supp_KEGG.txt",header=T,sep="t",stringsAsFactors=FALSE)
    pp <- ggplot(pathway,aes(richFactor,Pathway))
    pp + geom_point()
    pp + geom_point(aes(size=R0vsR3))
    pbubble = pp + geom_point(aes(size=R0vsR3+.5,color=-1*log10(Qvalue)))
    pbubble + scale_colour_gradient(low="green",high="red")
    pr = pbubble + scale_colour_gradient(low="green",high="red") + labs(color=expression(-log[10](Qvalue)),size="Gene number",x="Enrich factor",y="KEGG Pathway",title="Top10 of KEGG Pathway")
    pr + theme(axis.text.y = element_text(size = 16))

    数据分段

    1
    v %>% rank() %>% cut(breaks = 10)

    一页多图

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
    library(grid)


    plots <- c(list(...), plotlist)

    numPlots = length(plots)

    # If layout is NULL, then use 'cols' to determine layout
    if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
    ncol = cols, nrow = ceiling(numPlots/cols))
    }

    if (numPlots==1) {
    print(plots[[1]])

    } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
    # Get the i,j matrix positions of the regions that contain this subplot
    matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

    print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
    layout.pos.col = matchidx$col))
    }
    }
    }

    dplyr mutate以行计算需要Group input by rows,否则上下列会串(血的教训T_T)

    1
    rowwise()

    18.4.26

    终于找到ggplot2热图空行的解决方案了!!!facet里加参数scale = “free”

    1
    2
    3
    4
    ggplot() + 
    geom_tile() +
    scale_color_gradient2() +
    facet_wrap(~group, scales = "free")

    18.4.28

    origin

    < 大专栏  记录R的一些黑魔法td class="code">
    ## m=matrix(data=sample(rnorm(100,mean=0,sd=2)), ncol=10)
    ## this function makes a graphically appealing heatmap (no dendrogram) using ggplot
    ## whilst it contains fewer options than gplots::heatmap.2 I prefer its style and flexibility

    ggheat=function(m, rescaling='none', clustering='none', labCol=T, labRow=T, border=FALSE,
    heatscale= c(low='blue',high='red'))
    {
    ## the function can be be viewed as a two step process
    ## 1. using the rehape package and other funcs the data is clustered, scaled, and reshaped
    ## using simple options or by a user supplied function
    ## 2. with the now resahped data the plot, the chosen labels and plot style are built

    require(reshape2)
    require(ggplot2)

    ## you can either scale by row or column not both!
    ## if you wish to scale by both or use a differen scale method then simply supply a scale
    ## function instead NB scale is a base funct

    if(is.function(rescaling))
    {
    m=rescaling(m)
    }
    else
    {
    if(rescaling=='column')
    m=scale(m, center=T)
    if(rescaling=='row')
    m=t(scale(t(m),center=T))
    }

    ## I have supplied the default cluster and euclidean distance- and chose to cluster after scaling
    ## if you want a different distance/cluster method-- or to cluster and then scale
    ## then you can supply a custom function

    if(is.function(clustering))
    {
    m=clustering(m)
    }else
    {
    if(clustering=='row')
    m=m[hclust(dist(m))$order, ]
    if(clustering=='column')
    m=m[,hclust(dist(t(m)))$order]
    if(clustering=='both')
    m=m[hclust(dist(m))$order ,hclust(dist(t(m)))$order]
    }
    ## this is just reshaping into a ggplot format matrix and making a ggplot layer

    rows=dim(m)[1]
    cols=dim(m)[2]
    melt.m=cbind(rowInd=rep(1:rows, times=cols), colInd=rep(1:cols, each=rows) ,melt(m))
    g=ggplot(data=melt.m)

    ## add the heat tiles with or without a white border for clarity

    if(border==TRUE)
    g2=g+geom_rect(aes(xmin=colInd-1,xmax=colInd,ymin=rowInd-1,ymax=rowInd, fill=value),colour='white')
    if(border==FALSE)
    g2=g+geom_rect(aes(xmin=colInd-1,xmax=colInd,ymin=rowInd-1,ymax=rowInd, fill=value))

    ## add axis labels either supplied or from the colnames rownames of the matrix

    if(labCol==T)
    g2=g2+scale_x_continuous(breaks=(1:cols)-0.5, labels=colnames(m))
    if(labCol==F)
    g2=g2+scale_x_continuous(breaks=(1:cols)-0.5, labels=rep('',cols))

    if(labRow==T)
    g2=g2+scale_y_continuous(breaks=(1:rows)-0.5, labels=rownames(m))
    if(labRow==F)
    g2=g2+scale_y_continuous(breaks=(1:rows)-0.5, labels=rep('',rows))

    ## get rid of grey panel background and gridlines

    # g2=g2+opts(panel.grid.minor=theme_line(colour=NA), panel.grid.major=theme_line(colour=NA),
    # panel.background=theme_rect(fill=NA, colour=NA))

    ## finally add the fill colour ramp of your choice (default is blue to red)-- and return
    return(g2+scale_fill_continuous("", heatscale[1], heatscale[2]))

    }

    ## NB because ggheat returns an ordinary ggplot you can add ggplot tweaks post-production e.g.
    ## data(mtcars)
    ## x= as.matrix(mtcars)
    ## ggheat(x, clustCol=T)+ opts(panel.background=theme_rect(fill='pink'))
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87

    黑白屏恢复:win+ctrl+C

    18.5.3 pheatmap怎么拼图

    y叔:听说你还不会画heatmap

    1
    2
    x = pheatmap::pheatmap(d)
    cowplot::plot_grid(x$gtable, ...)

    18.5.24 GTF读取

    1
    rtracklayer::import() %>% as.data.frame()

    18.8.3 读取dataframe指定某一列数据类型

    1
    2
    read.table("xx", colClasses = c("id"="character"))
    fread("xx", ..., colClasses = c("id"="character"))

    18.12.4 object名与字符串互换

    1
    2
    get("object")
    deparse(substitute(object))
  • 相关阅读:
    Ubuntu 16.04 设置静态IP 注意事项
    C++ Primer: 1. 初识输入和输出
    车牌识别1:License Plate Detection and Recognition in Unconstrained Scenarios阅读笔记
    梳理检测论文-Refinement Neural Network
    linux 的 磁盘管理
    ubuntu 18 设置语言环境
    Ubuntu 18.04 的网络配置
    YoLo 实践(1)
    Distributed TensorFlow
    MXNet 分布式环境部署
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12256169.html
Copyright © 2020-2023  润新知