通路富集结果可视化

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))

记录R的一些黑魔法的更多相关文章

  1. If one session has a shared or exclusive lock on record R in an index, another session cannot insert

    If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...

  2. R的极客理想系列文章--转载

    http://blog.fens.me/series-r/ R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在 ...

  3. Rserve详解,R语言客户端RSclient【转】

    R语言服务器程序 Rserve详解 http://blog.fens.me/r-rserve-server/ Rserve的R语言客户端RSclient https://blog.csdn.net/u ...

  4. 如何制作自己的R包?

    摘自 方匡南 等编著<R数据分析-方法与案例详解>.电子工业出版社 R包简介 R包提供了一个加载所需代码.数据和文件的集合.R软件自身就包含大约30种不同功能的包,这些基本包提供了R软件的 ...

  5. R in Action(0) 开篇

    这几年数据挖掘的火热,也越来越多的人把R作为数据挖掘的一个辅助工具,据国际性组织kkguter统计有60%的人在挖掘过程中用到R工具,可见这个工具是多么的流行,对于数据统计.筛选以及画图绝对是神器.尽 ...

  6. 如何制作自己的R包

    如何制作自己的R包? 摘自 方匡南 等编著<R数据分析-方法与案例详解>.电子工业出版社 R包简介 R包提供了一个加载所需代码.数据和文件的集合.R软件自身就包含大约30种不同功能的包,这 ...

  7. DP 优化方法大杂烩 & 做题记录 I.

    标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...

  8. SQLite学习笔记(十二)&&虚拟机指令

    上篇文章简单讨论了虚拟机的原理,这篇文章我们详细讨论下指令,具体从几种典型的SQL语句来看看每种SQL对应的指令流,以及每个指令的含义.通过explain语句,可以看到语句对应的指令流:通过pragm ...

  9. 完整mybatis应用

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

随机推荐

  1. iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码

    iOS精选源码 如丝般顺滑的微信朋友圈(点赞,评论,图文混排表情,... 动态菜单第三版本:动态项,自适应方向 仿appstore首页滚动效果 iOS 透明导航栏方案 TransparentNavig ...

  2. C#通过窗体应用程序操作数据库(增删改查)

    为了体现面向对象的思想,我们把“增删改查”这些函数封装到一个数据库操作类里: 为了便于窗体程序与数据库之间进行数据交互,我们建一个具有数据库行数据的类,通过它方便的在窗体程序与数据库之间传输数据: 我 ...

  3. E. Arson In Berland Forest(思维,找二维阵列中的矩阵,二分)

    题:https://codeforces.com/contest/1262/problem/E 分析:预处理出阵列中的矩阵,然后二分答案还原题目的烧火过程,判断是否满足要求 #include<b ...

  4. tesseract系列(3) -- tesseract训练

    tessract的训练有个工具叫 jTessBoxEditor 1.jTessBoxEditor是用java写的,首先要装java的环境 jdk-8u191-windows-x64.exe 这个我想从 ...

  5. redis day02 下

    位图:是二进制数据(0101101010)2^32 强势点: 01_login :101110(比如:第一天登录,二天没登录) 传统的字符串解决方案中 记录用户登录日期  统计堪忧 01_login_ ...

  6. day41-进程-管道

    #1.管道Pipe:双向通信: from multiprocessing import Pipe p1,p2 = Pipe() p1.send('hello') print(p2.recv()) p2 ...

  7. android geendao简单使用

    引入依赖 implementation 'org.greenrobot:greendao:3.2.2'implementation 'com.github.yuweiguocn:GreenDaoUpg ...

  8. 【SpringCloud】Eureka入门与原理

    为了开发效率高效和业务逻辑清晰,越来越多的项目采用分布式系统.分布式最重要的就是注册中心了.Eureka是SpringCloud原生提供的注册中心,来look一波吧. 超光速入门 服务端 引入依赖: ...

  9. springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题

    有时候,当你把你的springboot项目打成可执行的jar,放在linux上启动时,发现启动超级慢: 这往往是因为springboot内置tomcat启动时实例化SecureRandom对象随机数策 ...

  10. OpenCV 使用FLANN进行特征点匹配

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...