-
方法一 使用gaston包
參考:
https://cran.r-project.org/web/packages/gaston/gaston.pdf
# Load data
data(AGT)
x <- as.bed.matrix(AGT.gen, AGT.fam, AGT.bim)
# Compute LD
ld.x <- LD(x, c(1,ncol(x)))
# Plot a tiny part of the LD matrix
LD.plot( ld.x[1:20,1:20], snp.positions = x@snps$pos[1:20] )
這里可以把x替換為一個(gè)普通的矩陣就好屁倔,
此處的ld.x是一個(gè)x軸和y軸一樣的矩陣(如下圖)邓线,代碼中選取了前20行和20列進(jìn)行畫(huà)圖。這個(gè)賦值一個(gè)snp.position就能得到snp在染色體的位置抖坪。
這里的snp.position就是一串?dāng)?shù)字。
> x@snps$pos[1:20]
[1] 230802015 230804773 230804822 230806365 230808005 230808246 230808425 230808712
[9] 230808744 230809858 230810362 230812038 230812156 230812182 230812329 230812458
[17] 230813314 230814083 230814668 230815197
畫(huà)圖的時(shí)候蛛枚,把想要的matrix替換進(jìn)去即可术荤,其他的參數(shù)如下:
Usage
LD.plot(LD, snp.positions, max.dist = Inf, depth = nrow(LD),
graphical.par = list(mar = c(0,0,0,0)), cex.ld, cex.snp,
polygon.par = list(border = "white"),
color.scheme = function(ld) rgb(1,1-abs(ld),1-abs(ld)),
write.snp.id = TRUE, write.ld = function(ld) sprintf("%.2f", ld),
draw.chr = TRUE, above.space = 1 + 2*write.snp.id + draw.chr
below.space = 1, pdf.file, finalize.pdf = TRUE)
考慮:參數(shù)中對(duì)于顏色的定義是: color.scheme = function(ld) rgb(1,1-abs(ld),1-abs(ld)),使用了rgb()函數(shù)斋泄,紅色漸變杯瞻,適合于0-1之間的值。
這里對(duì)于繪制數(shù)值只能是0-1之間是己,而我的值有正有負(fù)又兵。所以使用max(matrix)取了正值的最大值30, 使用abs(min(matrix))取了負(fù)值的最大值27卒废, 我修改了這里的function沛厨。
LD.plot(my_matrix, snp.positions = exon_position$exon_pos,
graphical.par = list(cex =0.1),
color.scheme = function(ld)
if(ld > 0) rgb(1,(30-min(30, ld)) / 30,(30-min(30, ld)) / 30) else
rgb((27-min(27, abs(ld))) / 27,(27-min(27, abs(ld))) / 27,1))
這里新學(xué)到一個(gè)函數(shù)par()∷と希可以看到LD.plot有一個(gè)graphical.par參數(shù)逆皮,是對(duì)圖性進(jìn)行修改的,但是我當(dāng)時(shí)沒(méi)理解這里具體怎么修改参袱,修改哪些成份电谣。后來(lái)發(fā)現(xiàn)R語(yǔ)言畫(huà)圖有par()函數(shù)。直接輸入par()就可以看到其中有哪些參數(shù)了抹蚀。這些參數(shù)和graphical.par是對(duì)應(yīng)的剿牺。
par()參考鏈接:
http://www.reibang.com/p/5d44a08f67a5
LD.plot( ld.x[1:20,1:20], snp.positions = x@snps$pos[1:20],
graphical.par = list(cex = 1.3, bg = "gray"),
polygon.par = list(border = NA), write.ld = NULL )
-
方法二: 使用ggplot2畫(huà)
參考視頻:
R語(yǔ)言ggplot2畫(huà)下三角熱圖展示相關(guān)系數(shù) - 知乎 (zhihu.com)
有空的時(shí)候把代碼補(bǔ)充一下。
- 方法三: 使用corrplot
cormatrix <- cor(mtcars)
library(corrplot)
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582","#FDDBC7",
"#FFFFFF", "#D1E5F0", "#92C5DE","#4393C3", "#2166AC",
"#053061"))(100) %>% rev()
corrplot(cormatrix, is.corr = FALSE,
method="circle", type='upper', outline = FALSE,
tl.pos = 'd', tl.col = "black", tl.offset = 1, tl.cex = 1,
cl.pos = 'r',
addCoef.col = T,
order = "original",
number.font = 6, col = col2)
對(duì)一些參數(shù)進(jìn)行解釋
這個(gè)函數(shù)參數(shù)很多环壤,可以完成的事情也相對(duì)多一些
參考網(wǎng)站:
R語(yǔ)言相關(guān)系數(shù)可視化之corrplot包 - 知乎 (zhihu.com)
corrplot(
corr,
method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
type = c("full", "lower", "upper"),
add = FALSE,
col = NULL,
cl.lim = NULL,
bg = "white",
title = "",
is.corr = TRUE,
diag = TRUE,
outline = FALSE,
mar = c(0, 0, 0, 0),
addgrid.col = NULL,
addCoef.col = NULL,
addCoefasPercent = FALSE,
order = c("original", "AOE", "FPC", "hclust", "alphabet"),
hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average",
"mcquitty", "median", "centroid"),
addrect = NULL,
rect.col = "black",
rect.lwd = 2,
tl.pos = NULL,
tl.cex = 1,
tl.col = "red",
tl.offset = 0.4,
tl.srt = 90,
cl.pos = NULL,
cl.length = NULL,
cl.cex = 0.8,
cl.ratio = 0.15,
cl.align.text = "c",
cl.offset = 0.5,
number.cex = 1,
number.font = 2,
number.digits = NULL,
addshade = c("negative", "positive", "all"),
shade.lwd = 1,
shade.col = "white",
p.mat = NULL,
sig.level = 0.05,
insig = c("pch", "p-value", "blank", "n", "label_sig"),
pch = 4,
pch.col = "black",
pch.cex = 3,
plotCI = c("n", "square", "circle", "rect"),
lowCI.mat = NULL,
uppCI.mat = NULL,
na.label = "?",
na.label.col = "black",
win.asp = 1,
...
)
當(dāng)你的數(shù)據(jù)有正有負(fù)的時(shí)候晒来, 設(shè)定is.corr = FALSE, 這樣顏色就會(huì)把范圍控制在你的數(shù)值之間郑现。我這里修改的顏色湃崩,將正值定為紅色荧降,負(fù)值定位藍(lán)色,這里使用的顏色col2 使用rev()使其顛倒過(guò)來(lái)攒读,再賦值給col參數(shù)朵诫。tl.pos= 'd',是確定titlte的位置,在對(duì)角線(xiàn)那里薄扁,這里以tl開(kāi)頭的都是控制title的剪返。cl.pos = 'r' 是控制色條的位置在右邊。tl.srt 可以賦值控制title的傾斜方向泌辫,但是好像在對(duì)角線(xiàn)處的title修改不了傾斜方向随夸。
如果各位大佬有其他的可以畫(huà)類(lèi)似圖形的方法,歡迎共同探討震放!