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)
[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 6.0 5.0
[15] 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 0.6 0.5 0.4 0.3
[29] 0.2 0.1 0.1 0.2 0.3 0.4 0.5 0.6 0.1 0.2 0.3 0.4 0.5 0.6
[43] 0.6 0.5 0.4 0.3 0.2 0.1 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0
[57] 4.0 3.0 2.0 1.0 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0
[71] 5.0 6.0 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0
[85] 1.0 2.0 3.0 4.0 5.0 6.0 6.0 5.0 4.0 3.0 2.0 1.0bash
> 1:3+4 [1] 5 6 7 > 1:(3+4) [1] 1 2 3 4 5 6 7
# ncol 指定列數 # byrow 先按行填充數據 # ?matrix 可查看函數的使用方法 # as.data.frame的as系列是轉換用的 data <- as.data.frame(matrix(data, ncol=12, byrow=T))
# 增長列的名字 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","OOcyte") # 增長行的名字 rownames(data) <- paste("Gene", 1:8, sep="_") # 只顯示前6行和前4列 head(data)[,1: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" # 習慣設置quote爲空,避免部分基因名字或註釋中存在引號,致使讀入文件錯誤。 > data2 <- read.table(text=txt, sep=";", header=T, row.names=1, quote="") > head(data2) 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
# 讀入時,增長一個參數`check.names=F`也能夠解決問題。 # 此次數字前沒有再加 X 了 > data2 <- read.table(text=txt, sep=";", header=T, row.names=1, quote="", check.names = F) > head(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
> data2 <- read.table("filename", sep=";", header=T, row.names=1, quote="")
# 若是包沒有安裝,運行下面一句,安裝包 #install.packages(c("reshape2","ggplot2","magrittr")) library(reshape2) library(ggplot2) # 轉換前,先增長一列ID列,保存行名字 data$ID <- rownames(data) # melt:把正常矩陣轉換爲長表格模式的函數。工做原理是把所有的非id列的數值列轉爲1列,命名爲value;全部字符列轉爲variable列。 # id.vars 列用於指定哪些列爲id列;這些列不會被merge,會保留爲完整一列。 data_m <- melt(data, id.vars=c("ID")) head(data_m)
ID variable value1 Gene_1 Zygote 1.02 Gene_2 Zygote 6.03 Gene_3 Zygote 0.64 Gene_4 Zygote 0.15 Gene_5 Zygote 1.06 Gene_6 Zygote 6.07 Gene_7 Zygote 6.08 Gene_8 Zygote 1.09 Gene_1 2_cell 2.010 Gene_2 2_cell 5.011 Gene_3 2_cell 0.512 Gene_4 2_cell 0.213 Gene_5 2_cell 2.014 Gene_6 2_cell 5.015 Gene_7 2_cell 5.016 Gene_8 2_cell 2.0
# data_m: 是前面費了九牛二虎之力獲得的數據表 # aes: aesthetic的縮寫,通常指定總體的X軸、Y軸、顏色、形狀、大小等 # 在最開始讀入數據時,通常只指定x和y,其它後續指定 p <- ggplot(data_m, aes(x=variable,y=ID)) # 熱圖就是一堆方塊根據其值賦予不一樣的顏色,因此這裏使用fill=value, 用數值作填充色。 p <- p + geom_tile(aes(fill=value)) # ggplot2爲圖層繪製,一層層添加,存儲在p中,在輸出p的內容時纔會出圖。 p ## 若是你沒有使用Rstudio或其它R圖形版工具,而是在遠程登陸的服務器上運行的交互式R,須要輸入下面的語句,得到輸出圖形(圖形存儲於R的工做目錄下的Rplots.pdf文件中)
# theme: 是處理圖美觀的一個函數,能夠調整橫縱軸label的選擇、圖例的位置等 # 這裏選擇X軸標籤45度。 # hjust和vjust調整標籤的相對位置,具體見 # 簡單說,hjust是水平的對齊方式,0爲左,1爲右,0.5居中,0-1之間能夠取任意值。vjust是垂直對齊方式,0底對齊,1爲頂對齊,0.5居中,0-1之間能夠取任意值 p <- p + theme(axis.text.x=element_text(angle=45, hjust=1, vjust=1)) p
# 連續的數字,指定最小數值表明的顏色和最大數值賦予的顏色 # 注意fill和color的區別,fill是填充,color只針對邊緣 p <- p + scale_fill_gradient(low = "white", high = "red") p
# postion能夠接受的值有 top, bottom, left, right, 和一個座標 c(0.05,0.8) (左上角,座標是相對於圖的左下角計算的) p <- p + theme(legend.position="top")
p <- p + xlab("samples") + theme_bw() + theme(panel.grid.major = element_blank()) + theme(legend.key=element_blank()) p
p <- ggplot(data_m, aes(x=variable,y=ID)) + xlab("samples") + theme_bw() + theme(panel.grid.major = element_blank()) + theme(legend.key=element_blank()) + theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1)) + theme(legend.position="top") + geom_tile(aes(fill=value)) + scale_fill_gradient(low = "white", high = "red")
# 能夠跟輸出文件不一樣的後綴,以得到不一樣的輸出格式 # colormode支持srgb (屏幕)和cmyk (打印,部分雜誌須要,看上去有點褪色的感受)格式 ggsave(p, filename="heatmap.pdf", width=10, height=15, units=c("cm"),colormodel="srgb")