```{r} library(ggplot2) CN_DT ...
library(ggplot2)
CN_DT <- fread("/home/ywliao/project/Gengyan/ONCOCNV_result/ONCOCNV_all_result.txt",sep="\t")
dt <- CN_DT[cfDNATime=="cfDNA1"]
wdt <- dcast(dt,Gene~Sample,value.var = "CN",fun.aggregate = mean)
data <- as.matrix(wdt[,2:length(wdt),with=F]) #數據矩陣
rownames(data) <- unlist(wdt[,1])
hc<-hclust(dist(data),method = "average")
rowInd<-hc$order
hc<-hclust(dist(t(data)),method = "average")
colInd<-hc$order
data<-data[rowInd,colInd] #聚類分析的作用是為了色塊集中,顯示效果好。如果本身就對樣品有分組,基因有排序,就可以跳過這一步。
dp=melt(data)
colnames(dp) <- c("Gene","Sample","Value")
p <- ggplot(dp, aes(Sample,Gene)) + geom_tile(aes(fill = as.factor(Value)))+theme(axis.text.x=element_text(angle = 90))+ guides(fill = guide_legend(title = "Copy Number")) + scale_fill_brewer(palette = 3)
p
library(gplots)
CN_DT <- fread("/home/ywliao/project/Gengyan/ONCOCNV_result/ONCOCNV_all_result.txt",sep="\t")
dt <- CN_DT[cfDNATime=="cfDNA1"]
wdt <- dcast(dt,Gene~Sample,value.var = "CN",fun.aggregate = mean)
dp <- as.matrix(wdt[,2:length(wdt),with=F])
labrow <- unlist(wdt[,1,with=F])
colorsChoice<- colorRampPalette(c("green","black","red"))
heatmap.2(dp,labRow = labrow,col=colorsChoice(5),breaks = c(1,1.5,2,2.5,3,4),density.info="histogram",
hclustfun = function(c)hclust(c,method="average"),keysize = 1.5, cexRow=0.5,trace = "none");