R语言学习-绘制热图初步
Posted swtwanzhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言学习-绘制热图初步相关的知识,希望对你有一定的参考价值。
> 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 Gene_1;1;2;3;4 Gene_2;6;5;4;5 Gene_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
以上是关于R语言学习-绘制热图初步的主要内容,如果未能解决你的问题,请参考以下文章