R語言學習 - 線圖繪製

線圖是反映趨勢變化的一種方式,其輸入數據通常也是一個矩陣。
單線圖
假設有這麼一個矩陣,第一列爲轉錄起始位點及其上下游5 kb的區域,第二列爲H3K27ac修飾在這些區域的丰度,想繪製一張線圖展現。
profile="Pos;H3K27ac
-5000;8.7
-4000;8.4
-3000;8.3
-2000;7.2
-1000;3.6
0;3.6
1000;7.1
2000;8.2
3000;8.4
4000;8.5
5000;8.5"
讀入數據
profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")

# 在melt時保留位置信息
# melt格式是ggplot2畫圖最喜歡的格式
# 好好體會下這個格式,雖然多佔用了很多空間,可是確實很方便
# 這裏能夠用 `xvariable`,也能夠是其它字符串,但須要保證後面與這裏的一致
# 由於這一列是要在X軸顯示,因此起名爲`xvariable`。
profile_text$xvariable = rownames(profile_text)
library(ggplot2)
library(reshape2)
data_m <- melt(profile_text, id.vars=c("xvariable"))
data_m
   xvariable variable value
1      -5000  H3K27ac   8.7
2      -4000  H3K27ac   8.4
3      -3000  H3K27ac   8.3
4      -2000  H3K27ac   7.2
5      -1000  H3K27ac   3.6
6          0  H3K27ac   3.6
7       1000  H3K27ac   7.1
8       2000  H3K27ac   8.2
9       3000  H3K27ac   8.4
10      4000  H3K27ac   8.5
11      5000  H3K27ac   8.5
而後開始畫圖,與畫heatmap同樣。
# variable和value爲矩陣melt後的兩列的名字,內部變量, variable表明了點線的屬性,value表明對應的值。
p <- ggplot(data_m, aes(x=xvariable, y=value), color=variable) + geom_line()
# 圖會存儲在當前目錄的Rplots.pdf文件中,若是用Rstudio,能夠不運行dev.off()
dev.off()
滿心期待一個倒鐘形曲線,結果出錯了。仔細看,出來一段提示
geom_path: Each group consists of only one observation. 
Do you need to adjust the group aesthetic?
原來默認ggplot2把每一個點都視做了一個分組,什麼都沒畫出來。而data_m中的數據都來源於一個分組H3K27ac,分組的名字爲variable,修改下腳本,看看效果。
p <- ggplot(data_m, aes(x=xvariable, y=value, color=variable, group=variable)) + geom_line() + theme(legend.position=c(0.1,0.9))
p
圖出來了,一條線,看一眼沒問題;再仔細看,不對了,怎麼還不是倒鐘形,原來橫座標錯位了。
檢查下數據格式
summary(data_m)
  xvariable      variable        
 Length:11       H3K27ac:11     
 Class :character             
 Mode  :character
問題來了,xvariable雖然看上去像數字,但存儲的實際是字符串 (由於是做爲行名字讀取的),須要轉換爲數字。
data_m$xvariable <- as.numeric(data_m$xvariable)

#再檢驗下
is.numeric(data_m$xvariable)
[1] TRUE
好了,繼續畫圖。
# 注意斷行時,加號在行尾,不能放在行首
p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
     geom_line() + theme(legend.position=c(0.1,0.8))
p
圖終於出來了,調了下legend的位置,看上去有點意思了
有點難看,若是平滑下,會不會好一些,stat_smooth能夠對繪製的線進行局部擬合。在不影響變化趨勢的狀況下,可使用 (但慎用)。
p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) + 
     geom_line() + stat_smooth(method="auto", se=FALSE) + 
     theme(legend.position=c(0.1,0.8))
p
從圖中看,趨勢仍是一致的,線條更優美了。另一個方式是增長區間的數量,線也會好些,並且更真實。
stat_smooth和geom_line各繪製了一條線,只保留一條就好。
p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) + 
     stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.1,0.8))
p
好了,終於完成了單條線圖的繪製
多線圖
那麼再來一個多線圖的例子吧,只要給以前的數據矩陣多加幾列就行了。
profile = "Pos;h3k27ac;ctcf;enhancer;h3k4me3;polII
-5000;8.7;10.7;11.7;10;8.3
-4000;8.4;10.8;11.8;9.8;7.8
-3000;8.3;10.5;12.2;9.4;7
-2000;7.2;10.9;12.7;8.4;4.8
-1000;3.6;8.5;12.8;4.8;1.3
0;3.6;8.5;13.4;5.2;1.5
1000;7.1;10.9;12.4;8.1;4.9
2000;8.2;10.7;12.4;9.5;7.7
3000;8.4;10.4;12;9.8;7.9
4000;8.5;10.6;11.7;9.7;8.2
5000;8.5;10.6;11.7;10;8.2"

profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")

profile_text$xvariable = rownames(profile_text)
data_m <- melt(profile_text, id.vars=c("xvariable"))
data_m$xvariable <- as.numeric(data_m$xvariable)

# 這裏group=variable,而不是group=1 (若是上面你用的是1的話)
# variable和value爲矩陣melt後的兩列的名字,內部變量, variable表明了點線的屬性,value表明對應的值。
p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) + stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.85,0.2))
p
橫軸文本線圖
若是橫軸是文本,又該怎麼調整順序呢?還記得以前熱圖旁的行或列的順序調整嗎?從新設置變量的factor水平就能夠控制其順序。
profile = "Pos;h3k27ac;ctcf;enhancer;h3k4me3;polII
-5000;8.7;10.7;11.7;10;8.3
-4000;8.4;10.8;11.8;9.8;7.8
-3000;8.3;10.5;12.2;9.4;7
-2000;7.2;10.9;12.7;8.4;4.8
-1000;3.6;8.5;12.8;4.8;1.3
0;3.6;8.5;13.4;5.2;1.5
1000;7.1;10.9;12.4;8.1;4.9
2000;8.2;10.7;12.4;9.5;7.7
3000;8.4;10.4;12;9.8;7.9
4000;8.5;10.6;11.7;9.7;8.2
5000;8.5;10.6;11.7;10;8.2"

profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")

profile_text_rownames <- row.names(profile_text)

profile_text$xvariable = rownames(profile_text)
data_m <- melt(profile_text, id.vars=c("xvariable"))

# 就是這一句,會常常用到
data_m$xvariable <- factor(data_m$xvariable, levels=profile_text_rownames, ordered=T)

# geom_line設置線的粗細和透明度
p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) + geom_line(size=1, alpha=0.9) + theme(legend.position=c(0.85,0.2)) + theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1))
 
# stat_smooth
#p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) + stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.85,0.2)) + theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1))
p
比較下位置信息作爲數字(前面的線圖)和位置信息橫軸的差異。當爲數值時,ggplot2會選擇合適的幾個刻度作標記,當爲文本時,會所有標記。另外文本橫軸,smooth效果不明顯 

到此完成了線圖的基本繪製,雖然還能夠,但還有很多須要提升的地方,好比在線圖上加一條或幾條垂線、加個水平線、修改X軸的標記(好比0換爲TSS)、設置每條線的顏色等。bash

相關文章
相關標籤/搜索