首页 / 更多教程 / R语言学习-绘制热图初步
R语言学习-绘制热图初步
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了R语言学习-绘制热图初步,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6595字,纯文字阅读大概需要10分钟。
内容图文
![R语言学习-绘制热图初步](/upload/InfoBanner/zyjiaocheng/1296/b2f3cfe89f8e46eb965215389eb5382c.jpg)
> txt <- "ID;Zygote;2_cell;4_cell;8_cell + Gene_1;1;2;3;4 + Gene_2;6;5;4;5 + Gene_3;0.6;0.5;0.4;0.4" > txt [1] "ID;Zygote;2_cell;4_cell;8_cell\nGene_1;1;2;3;4\nGene_2;6;5;4;5\nGene_3;0.6;0.5;0.4;0.4" > data2 <- read.table(text = txt,sep = ";",header = T,row.names = 1) ##还可以这样创建数据框呢 > data2 ##列名中以数字开头的列名都加上了X。所以要尽量避免命名时以数字开头。加上check.names = F 可取消加X。 Zygote X2_cell X4_cell X8_cell Gene_1 1.0 2.0 3.0 4.0 Gene_2 6.0 5.0 4.0 5.0 Gene_3 0.6 0.5 0.4 0.4 > data2 <- read.table(text = txt,sep = ";",header = T,row.names = 1,check.names = F) > data2 Zygote 2_cell 4_cell 8_cell Gene_1 1.0 2.0 3.0 4.0 Gene_2 6.0 5.0 4.0 5.0 Gene_3 0.6 0.5 0.4 0.4
> txt <- "ID;Zygote;2_cell;4_cell;8_cell; ##这里句尾不能有分号 + + Gene_1;1;2;3;4 + + Gene_2;6;5;4;5 + + Gene_3;0.6;0.5;0.4;0.4" > data3 <- read.table(text = txt,sep = ";",header = T,row.names = 1) Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 6 elements
##开始创建数据框
#先用matrix()将vector转变为矩阵,在as.data.frame()
#ncol:指定列数
#byrow = T:先按行填充vector内容,我还以为有bycol,后面试了下,没有这个参数!_!
> data <- c(1:6,6:1,6:1,1:6,(6:1)/10,(1:6)/10,(1:6)/10,(6:1)/10,1:6,6:1,6:1,1:6,6:1,1:6,1:6,6:1) > data <- as.data.frame(matrix(data,ncol = 12,byrow = T)) > data V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 3.0 2.0 1.0 2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 0.4 0.5 0.6 4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 0.3 0.2 0.1 5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 3.0 2.0 1.0 6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 3.0 2.0 1.0 > data_bycol <- c(1:6,6:1,6:1,1:6,(6:1)/10,(1:6)/10,(1:6)/10,(6:1)/10,1:6,6:1,6:1,1:6,6:1,1:6,1:6,6:1) > data <- as.data.frame(matrix(data_bycol,ncol = 12,bycol = T)) Error in matrix(data_bycol, ncol = 12, bycol = T) : 参数没有用(bycol = T)
#增加列名
#colnames()
> colnames(data) <- c("Zygote","2_cell","4_cell","8_cell","Morula","ICM","ESC","4 week PGC","7 week PGC","10 week PGC","17 week PGC","00cyte") > data Zygote 2_cell 4_cell 8_cell Morula ICM ESC 4 week PGC 7 week PGC 1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 10 week PGC 17 week PGC 00cyte 1 3.0 2.0 1.0 2 4.0 5.0 6.0 3 0.4 0.5 0.6 4 0.3 0.2 0.1 5 3.0 2.0 1.0 6 4.0 5.0 6.0 7 4.0 5.0 6.0 8 3.0 2.0 1.0
#增加行名
#rownames() > rownames(data) <- paste("Gene",1:8,sep = "_") > data Zygote 2_cell 4_cell 8_cell Morula ICM ESC 4 week PGC 7 week PGC Gene_1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 Gene_4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 Gene_5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 10 week PGC 17 week PGC 00cyte Gene_1 3.0 2.0 1.0 Gene_2 4.0 5.0 6.0 Gene_3 0.4 0.5 0.6 Gene_4 0.3 0.2 0.1 Gene_5 3.0 2.0 1.0 Gene_6 4.0 5.0 6.0 Gene_7 4.0 5.0 6.0 Gene_8 3.0 2.0 1.0 > class(data) [1] "data.frame"
###paste(a,b,c...,sep = " ")
###连接a b c...多个R对象,将其转变为字符串之后连起来
> paste(1:12, c("st", "nd", "rd", rep("th", 9))) [1] "1 st""2 nd""3 rd""4 th""5 th""6 th""7 th""8 th" [9] "9 th""10 th""11 th""12 th" > paste(1:12, c("st", "nd", "rd", rep("th", 9)),sep = "") [1] "1st""2nd""3rd""4th""5th""6th""7th""8th""9th""10th" [11] "11th""12th" > paste0(1:12, c("st", "nd", "rd", rep("th", 9))) ##看起来paste0()和paste(...,sep = "")是等价的 [1] "1st""2nd""3rd""4th""5th""6th""7th""8th""9th""10th" [11] "11th""12th" > paste(1:12, c("st", "nd", "rd", rep("th", 9)),13:24,sep = "") [1] "1st13""2nd14""3rd15""4th16""5th17""6th18""7th19" [8] "8th20""9th21""10th22""11th23""12th24"
###转换数据格式
#先安装并加载所需R包
> install.packages(c("reshape2","ggplot2")) ##复习一下同时安装多个包
> library(reshape2)
> library(ggplot2)
#转换数据之前,给data加上一列,使之与行名一致。
> data$ID <- rownames(data)##直接$ID创建新列,然后通过rownames()或者data的行名,赋值给data。有没有其他方式?我这里想了想用cbind()应该怎么实现 > data Zygote 2_cell 4_cell 8_cell Morula ICM ESC 4 week PGC 7 week PGC Gene_1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 Gene_4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 Gene_5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 10 week PGC 17 week PGC 00cyte ID Gene_1 3.0 2.0 1.0 Gene_1 Gene_2 4.0 5.0 6.0 Gene_2 Gene_3 0.4 0.5 0.6 Gene_3 Gene_4 0.3 0.2 0.1 Gene_4 Gene_5 3.0 2.0 1.0 Gene_5 Gene_6 4.0 5.0 6.0 Gene_6 Gene_7 4.0 5.0 6.0 Gene_7 Gene_8 3.0 2.0 1.0 Gene_8 > data2 <- cbind(data,rownames(data))##没想到直接把rownames(data)作为新列名了。。。所以下面新建了一个列名 > data2 Zygote 2_cell 4_cell 8_cell Morula ICM ESC 4 week PGC 7 week PGC Gene_1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 Gene_4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 Gene_5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 10 week PGC 17 week PGC 00cyte ID rownames(data) Gene_1 3.0 2.0 1.0 Gene_1 Gene_1 Gene_2 4.0 5.0 6.0 Gene_2 Gene_2 Gene_3 0.4 0.5 0.6 Gene_3 Gene_3 Gene_4 0.3 0.2 0.1 Gene_4 Gene_4 Gene_5 3.0 2.0 1.0 Gene_5 Gene_5 Gene_6 4.0 5.0 6.0 Gene_6 Gene_6 Gene_7 4.0 5.0 6.0 Gene_7 Gene_7 Gene_8 3.0 2.0 1.0 Gene_8 Gene_8 > data2 <- cbind(data,ID_2 = rownames(data)) ##bingo! > data2 Zygote 2_cell 4_cell 8_cell Morula ICM ESC 4 week PGC 7 week PGC Gene_1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_2 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_3 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 Gene_4 0.1 0.2 0.3 0.4 0.5 0.6 0.6 0.5 0.4 Gene_5 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 Gene_6 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_7 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 Gene_8 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 10 week PGC 17 week PGC 00cyte ID ID_2 Gene_1 3.0 2.0 1.0 Gene_1 Gene_1 Gene_2 4.0 5.0 6.0 Gene_2 Gene_2 Gene_3 0.4 0.5 0.6 Gene_3 Gene_3 Gene_4 0.3 0.2 0.1 Gene_4 Gene_4 Gene_5 3.0 2.0 1.0 Gene_5 Gene_5 Gene_6 4.0 5.0 6.0 Gene_6 Gene_6 Gene_7 4.0 5.0 6.0 Gene_7 Gene_7 Gene_8 3.0 2.0 1.0 Gene_8 Gene_8
原文:https://www.cnblogs.com/SWTwanzhu/p/12951166.html
内容总结
以上是互联网集市为您收集整理的R语言学习-绘制热图初步全部内容,希望文章能够帮你解决R语言学习-绘制热图初步所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。