R-可视化-韦恩图venn
示例数据 3个基因集列表,为list格式 1 2 3 4 5 genes <- paste0("gene",1:1000) set.seed(20210302) gene_list <- list(A = sample(genes,100), B = sample(genes,200), C = sample(genes,300)) 一、VennDiagram VennDiagram包是绘制韦恩图的一个经典包。 ...
示例数据 3个基因集列表,为list格式 1 2 3 4 5 genes <- paste0("gene",1:1000) set.seed(20210302) gene_list <- list(A = sample(genes,100), B = sample(genes,200), C = sample(genes,300)) 一、VennDiagram VennDiagram包是绘制韦恩图的一个经典包。 ...
一、ggplot绘制基础箱图 0、示例数据 1 2 3 4 5 6 7 8 9 10 11 12 13 library(ggplot2) library(patchwork) #组别名最好是字符型;如果是数值类型,最好转为因子化 ToothGrowth$dose = factor(ToothGrowth$dose) summary(ToothGrowth) # len supp dose # Min. : 4.20 OJ:30 0.5:20 # 1st Qu.:13.07 VC:30 1 :20 # Median :19.25 2 :20 # Mean :18.81 # 3rd Qu.:25.27 # Max. :33.90 1、基础绘图 1 2 3 4 5 p1 = ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_boxplot() p2 = ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) + geom_boxplot() p1 | p2 2、离群点相关 1 2 3 4 5 6 p1 = ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(outlier.color = "red", outlier.size = 0.5) p2 = ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(outlier.alpha = 0) #透明度为0,相当于不绘制离群点 p1 + p2 ...
1、示例数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 library(ggplot2) library(patchwork) library(carData) #示例数据 head(Salaries) #教职工资情况 # rank discipline yrs.since.phd yrs.service sex salary # 1 Prof B 19 18 Male 139750 # 2 Prof B 20 16 Male 173200 # 3 AsstProf B 4 3 Male 79750 # 4 Prof B 45 39 Male 115000 # 5 Prof B 40 41 Male 141500 # 6 AssocProf B 6 6 Male 97000 table(Salaries$rank, Salaries$sex) # Female Male # AsstProf 11 56 # AssocProf 10 54 # Prof 18 248 2、基础用法 1 2 3 4 5 6 7 8 9 p1 = ggplot(Salaries, aes(x=rank)) + geom_bar() # 贴近x轴 p2 = ggplot(Salaries, aes(x=rank)) + geom_bar() + scale_y_continuous(expand=c(0,0)) # 映射填充颜色 p3 =ggplot(Salaries, aes(x=rank, fill=rank)) + geom_bar() p1 + p2 + p3 3、position=参数调整分组形式 1 2 3 4 5 6 7 8 # 默认 p1 <- ggplot(Salaries, aes(x=rank, fill=sex)) + geom_bar(position="stack") + labs(title='position="stack"') p2 <- ggplot(Salaries, aes(x=rank, fill=sex)) + geom_bar(position="dodge") + labs(title='position="dodge"') p3 <- ggplot(Salaries, aes(x=rank, fill=sex)) + geom_bar(position="fill") + labs(title='position="fill"') p1 + p2 + p3 + plot_layout(guides = 'collect') 4、stat=参数设置频数统计方式 stat="count"(default) 表示从给定的数据里,统计每个类别出现的次数; 此时aes()只需要给定x参数即可; stat="identity"表示直接指定每种类别的频数; 此时aes()除了需要给定x参数交代类别,还需要指定y参数表示频数值。 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 library(tidyverse) dat = Salaries %>% group_by(rank) %>% dplyr::summarise(n=n()) %>% as.data.frame() dat # rank n # 1 AsstProf 67 # 2 AssocProf 64 # 3 Prof 266 p1 = ggplot(dat, aes(x=rank, y=n, fill=rank)) + geom_bar(stat = "identity") dat = Salaries %>% group_by(rank,sex) %>% dplyr::summarise(n=n()) %>% as.data.frame() dat # rank sex n # 1 AsstProf Female 11 # 2 AsstProf Male 56 # 3 AssocProf Female 10 # 4 AssocProf Male 54 # 5 Prof Female 18 # 6 Prof Male 248 p2 = ggplot(dat, aes(x=rank, y=n, fill=sex)) + geom_bar(stat = "identity") p1 + p2 5、geom_text()添加频数注释 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dat = Salaries %>% group_by(rank) %>% dplyr::summarise(n=n()) p1=ggplot(dat, aes(x=rank, y=n)) + geom_bar(stat="identity") + geom_text(aes(label=n), vjust = -0.2) # vjust<0,上移;vjust>0,下移 dat = Salaries %>% group_by(rank,sex) %>% dplyr::summarise(n=n()) p2=ggplot(dat, aes(x=rank, y=n, fill=sex)) + geom_bar(stat="identity", position = "dodge") + geom_text(aes(label=n), vjust = -0.2, position=position_dodge(width=0.9)) p1 + p2 6、调整柱子的顺序 如果只有一种分组方式,通过设置类别的因子水平即可。 或者使用scale_x_discrete(c(.....))自定义顺序也可以实现很方便的修改 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ggplot(Salaries, aes(x=rank)) + geom_bar() + scale_x_discrete(limits=c("AsstProf", "AssocProf", "Prof")) p1=ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot() + ggtitle("default factor levels") p2=ggplot(iris, aes(x=fct_reorder(Species, Sepal.Width), y=Sepal.Width)) + geom_boxplot() + ggtitle("fct_reorder default levels") p3=ggplot(iris, aes(x=fct_reorder(Species, Sepal.Width, .desc=T), y=Sepal.Width)) + geom_boxplot() + ggtitle("fct_reorder descent levels") library(patchwork) p1 | p2 | p3 但如果更复杂的情况–组内排序。 例:5个学生的三门课程成绩,按照每门学科分组,将5个学生按照成绩从低到高排序(或者从高到低排序)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 grade = data.frame( subject=rep(c("Chineses","Math","English"), each=5), name=rep(c("A","B","C","D","E"),3), score=c(79,65,70,94,82,76,87,80,81,89,88,79,82,95,90)) # 先按学科均分从高到低 # 然后每个学科内,成绩从低到高学生排序 grade$subject=fct_reorder(grade$subject, grade$score, .desc=T) library(tidytext) p1 = ggplot(grade, aes(x=reorder_within(name,score,subject), y=score, fill=name)) + geom_bar(stat = "identity") + scale_x_reordered() + facet_wrap(subject~. ,scales = "free_x") # 先按学科均分从低到高 # 然后每个学科内,成绩从高到低学生排序 grade$subject=fct_reorder(grade$subject, grade$score, .desc=F) library(tidytext) p2 = ggplot(grade, aes(x=reorder_within(name,-score,subject), y=score, fill=name)) + geom_bar(stat = "identity") + scale_x_reordered() + facet_wrap(subject~. ,scales = "free_x") p1 + p2 + plot_layout(guides = 'collect') 注意reorder_within(个体,值,分组),还需要设置scale_x_reordered() , facet_wrap(variable~. ,scales = "free_x") ...
1、R中颜色的表示方式 1.1 颜色的名字 R内置了657种颜色的名字可供调用 http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf 1 2 3 4 5 6 7 8 9 10 11 str(colors()) # chr [1:657] "white" "aliceblue" "antiquewhite" ... head(colors()[1:10]) # [1] "white" "aliceblue" "antiquewhite" # [4] "antiquewhite1" "antiquewhite2" "antiquewhite3" set.seed(111) cols = sample(colors(), 5) numbers = 1:5; names(numbers) = cols barplot(numbers, col=cols) 关于透明色,虽然不在这657个颜色当中,可以通过transparent指定。 ...
简单整理两个绘制热图R包的用法,分别是基础的pheatmap包与复杂的ComplexHeatmap包。 pheatmap 1 2 3 4 5 # install.packages("pheatmap") library(pheatmap) packageVersion("pheatmap") # [1] ‘1.0.12’ 0、 示例数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 exp = matrix(rnorm(300), nrow = 30, ncol = 10) set.seed(123) exp[1:15, 1:5] = exp[1:15, 1:5] + matrix(rnorm(75,mean = 4), nrow = 15, ncol = 5) set.seed(123) exp[16:30, 6:10] = exp[16:30, 6:10] + matrix(rnorm(75,mean = 3), nrow = 15, ncol = 5) exp = round(exp, 2) colnames(exp) = paste("Sample", 1:10, sep = "") rownames(exp) = paste("Gene", 1:30, sep = "") dim(exp) # [1] 30 10 head(exp) # Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10 # Gene1 4.47 5.74 5.56 3.18 6.38 0.06 -0.09 0.13 0.70 -1.75 # Gene2 3.49 3.71 2.24 4.23 4.10 -0.70 1.08 0.22 -0.11 0.10 # Gene3 4.34 0.37 5.64 3.05 2.42 -0.72 0.63 1.64 -1.26 -0.57 # Gene4 4.25 4.32 6.79 5.30 2.37 0.88 -0.11 -0.22 1.68 -0.97 # Gene5 3.99 4.45 3.38 4.29 1.74 -1.02 -1.53 0.17 0.91 -0.18 # Gene6 5.72 2.36 5.39 4.04 6.50 1.96 -0.52 1.17 0.24 1.01 ##基础绘图 pheatmap(exp) 1、聚类相关参数 下述均是针对行row的操作,改为col即为针对列的操作。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## (1) 不聚类 pheatmap(exp, cluster_row = FALSE) ## (2) 聚类但显示聚类树 pheatmap(exp, treeheight_row = 0) ## (3) 距离计算公式 clustering_distance_rows = "euclidean" # "correlation" ## (4) 聚类间距离计算方法 clustering_method = "average" #"ward.D2", "single", "complete"等 ## (5) 获取聚类后热图的表达矩阵(改变了行列顺序) ph = pheatmap(exp) ph$tree_row$order ph$tree_col$order ph_exp = exp[ph$tree_row$order, ph$tree_col$order] ph_exp[1:4,1:4] # Sample3 Sample5 Sample2 Sample1 # Gene26 1.05 -0.57 1.44 -0.71 # Gene19 -1.01 -0.52 -0.26 -0.63 # Gene24 0.98 -1.24 -0.96 -0.24 # Gene18 0.33 1.23 -0.49 0.24 2、颜色相关 1 2 3 4 5 6 7 8 9 10 11 #Default colours = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(100) str(colours) # chr [1:100] "#4575B4" "#4979B6" "#4E7DB8" "#5282BB" "#5786BD" "#5C8BBF" "#608FC2" ... # 个性化修改 colours = colorRampPalette(c("navy", "white", "firebrick3"))(10) #colours = colorRampPalette(c("#3288bd", "white", "#d53e4f"))(10) str(colours) # chr [1:10] "#3288BD" "#5FA2CB" "#8DBCDA" "#BAD7E9" "#E8F1F7" "#FAE9EB" "#F1BEC4" ... pheatmap(exp, color = colours) ...
弦图常用于表示两组或多组成员之间的连接关系。如下简单学习circlize包ChordDiagram()绘制弦图的基础用法 参考教程:https://jokergoo.github.io/circlize_book/book/the-chorddiagram-function.html 0、数据输入 对于matrix: ...
桑基图(sankey plot)是一种特定类型的流图,用于描述一组值到另一组值的流向;理论上来表示前一组值与后一组值存在一定的逻辑关系。从泛化角度来看,也可用于呈现多组间的构成比例关系。从这个角度来看,冲积图(alluvial plot)就是一种特殊的桑基图。 ...
1、pie() pie()是基础的绘制饼图函数,用法简单 1 2 3 4 5 6 7 8 pie(Prop, labels = LETTERS[1:5], main = "Test Piechart") # labels:设置标签 # radius:设置半径,default 0.8 # clockwise:设置是否顺时针,默认为F # init.angle:设置起始单元的角度 # col:设置颜色 # border与lty分别设置边的颜色与形状 ...
1、ggplot geom_text()通过交代文本的坐标位置与内容绘制,通常与点图联用。相关参数包括: color 标签的颜色 alpha 标签的透明度 check_overlap 若有重叠的标签则不显示,默认为False 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 library(tidyverse) library(patchwork) p = ggplot(mpg, aes(displ, hwy)) + geom_point() # 设置候选标签 best_in_class <- mpg %>% group_by(class) %>% filter(row_number(desc(hwy)) == 1) # nudge_x / nudge_y:设置标签的相对位置,移动单位与x/y轴匹配;大于0分别表示右、上 # hjust / vjust:设置标签的相对位置,移动单位与自身长宽匹配;小于0分别表示右,上 p1 = p + geom_text(aes(label = model), data = best_in_class) p2 = p + geom_text(aes(label = model), data = best_in_class, nudge_x = 1, nudge_y = 10) p1 | p2 geom_label()在上述的基础上设置文本框,相关参数包括: label.size:框轮廓线的宽度 label.r:框轮廓的平滑度 label.padding:框的大小 alpha:框的透明度 1 2 3 4 p1 = p + geom_label(aes(label = model), data = best_in_class) p2 = p + geom_label(aes(label = model), data = best_in_class, alpha=0.5, fill="grey", color="red") p1 | p2 2、ggrepel 上述函数对于重叠标签的处理能力有限,ggrepel包可对此进行有效的补充。简单理解就是确定文本标签的坐标后,对于存在互相重叠的标签进行适当偏移,使各自均能完整的显示。 ...
1 2 3 4 5 6 # install.packages("corrplot") library(corrplot) # 计算相关性矩阵 M<-cor(mtcars) dim(M) # [1] 11 11 1、可视化R系数方式 method参数提供了如下6种可视化方式 circle(default), square, ellipse, number, shade, color, pie 1 corrplot(M, method="ellipse") 2、对称布局方式 type参数提供了如下3种布局方式 full(default), upper, lower 1 corrplot(M, type="lower") 3、样本排序 order参数提供了如下 original(default), AOE, FPC, hclust, alphabet 如果采用hclust,可以通过hclust.method参数设置聚类方法 1 corrplot(M, order="hclust") 4、颜色方案 (1)相关性颜色 1 2 3 col = colorRampPalette(c("blue", "white", "red"))(20) # 连续型 col = RColorBrewer::brewer.pal(n=8, name="RdBu") #离散型 corrplot(M, col=col) (2)其它颜色 1 2 3 4 5 # 背景填充颜色 corrplot(M, bg="grey") # 方格边框颜色 corrplot(M, addgrid.col = "black") 5、文本属性设置 (1)坐标轴名:通过tl.系列参数设置大小,颜色、角度等 1 corrplot(M, tl.col="black", tl.srt=45) (2)标注相关性系数 1 corrplot(M, addCoef.col = "black", number.digits = 1) (3)标注P值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ## 首先得到P值矩阵 cor.mtest <- function(mat, ...) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(NA, n, n) diag(p.mat) <- 0 for (i in 1:(n - 1)) { for (j in (i + 1):n) { tmp <- cor.test(mat[, i], mat[, j], ...) p.mat[i, j] <- p.mat[j, i] <- tmp$p.value } } colnames(p.mat) <- rownames(p.mat) <- colnames(mat) p.mat } p.mat <- cor.mtest(mtcars) dim(p.mat) # [1] 11 11 sig.level 参数设置显著阈值 insig参数设置标注方式,可选项包括 pch(default):将不显著的,标注为叉 blank:将不显著的,不显示 n:不作处理 label_sig 将显著的,标注星号 1 corrplot(M, p.mat = p.mat, sig.level = 0.01, insig = "blank") 1 2 3 corrplot(M, p.mat = p.mat, sig.level = c(0.1, 0.01), insig = "label_sig", pch.cex = 1.5, pch.col = "red") 6、其它参数 is.corr参数, 默认为TRUE;即输入矩阵是否为相关性矩阵 1 corrplot(scale(t(mtcars)), is.corr = FALSE) diag参数,默认为TRUE;即是否显示对角线的相关性 1 corrplot(M, diag=FALSE)