Combined Plot
(CH slack, June 11th 2020 - June 16th 2020)
(CH slack, June 11th 2020 - June 16th 2020)
R code location on server: /space/chen-syn01/1/data/cinliu/plots/circle_heatmap
Data location on server:
"/space/chen-syn01/1/data/wed009/gwas_chromatin/pheno_celltype_heatmap/allregions_ldsc_celltypes_coefficientzscore.txt" (using colors)
"/space/chen-syn01/1/data/wed009/gwas_chromatin/pheno_celltype_heatmap/gChromVar_MOp_organoid_heatmap_mat.txt" (using circle size)
Data given: two .txt files (files ready for heatmap)
#setup
rm(list=ls())
#install.packages('reshape')
library(ggplot2)
library(gplots)
library(reshape)
setwd("/Users/niniliu/Desktop/2020lab")
DDir='/Users/niniliu/Desktop/2020lab'
import the two .txt files
circleDat <- read.delim("~/Desktop/2020lab/gChromVar_MOp_organoid_heatmap_mat.txt")
#View(gChromVar_MOp_organoid_heatmap_mat)
colorDat <- read.delim("~/Desktop/2020lab/allregions_ldsc_celltypes_coefficientzscore.txt")
#View(allregions_ldsc_celltypes_coefficientzscore)
circleDat has 30 objects, 26 vraiables
colorDat has 20 objects, 25 vraiables
In order for this plot to work we need to have the same number of x and y variables in the same order.
Steps for this plot's data manipulation:
a. adjust colorDat
b. adjust circleDat
c. combine the 2 tables into 1 table
Order the variables:
Make a separate ordered list that has the order you want
#change order of colorDAt
##colorDat$CellTypes
orderlist <- c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT", "Ex_L5-6_NP", "Ex_L5_ET",
"Ex_L5_IT", "Ex_L6b", "Ex_L6_CT", "Ex_L6_IT_Car3", "Ex_L6_IT", "In_LAMP5", "In_PVALB",
"In_SNCG", "In_SST_CHODL", "In_SST", "In_VIP", "In_VLMC")
Match the name to the order list you made.
match(colorDat$CellTypes, orderlist)
rownames(colorDat) <- match(colorDat$CellTypes, orderlist) #add a new column that specpfiy the desired order
colorDat$order <- match(colorDat$CellTypes, orderlist)
Make sure the variables have the exact same names:
Adjust the names so the 2 data frames use the same names, save as new df
colorDat$CellTypes <- factor(colorDat$CellTypes,
levels = c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT","Ex_L5-6_NP", "Ex_L5_ET","Ex_L5_IT", "Ex_L6b", "Ex_L6_CT", "Ex_L6_IT_Car3", "Ex_L6_IT", "In_LAMP5", "In_PVALB","In_SNCG", "In_SST_CHODL", "In_SST", "In_VIP", "In_VLMC"))
col.df <- colorDat[order(colorDat$order),] #the df will work with
Changes here are similar to the above changes with the exacption that circleDat has more columns then colorDat, thus there's an extra step of selecting for the desired data so the 2 tables have matching numbers of variables in x and y.
#make dat match circle
circleDat[0,1:20] #cirle 1-20 collumn
circle.df <- as.data.frame(t(circleDat[1:24, 1:20])) #make short list and turn
corr_nameslist <- c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT", "Ex_L5_ET",
"Ex_L5_IT", "Ex_L5-6_NP", "Ex_L6_CT", "Ex_L6_IT", "Ex_L6_IT_Car3","Ex_L6b",
"In_LAMP5", "In_PVALB", "In_SNCG", "In_SST", "In_SST_CHODL", "In_VIP", "In_VLMC")
circle.df$names <-corr_nameslist #this is the circlces name list, corrected with minor differenes with other name list
#random check
circle.df[20,]
#match and organize names
circle.df$order <- match(circle.df$names, orderlist)
cirorder <- circle.df[order(circle.df$order),] #reorder
cir.df <- cirorder[0:20,1:24] #clean out the order row
#get rid of order column in col.df
color.df <- col.df[,0:25]
Using the melt() function in 'reshape' package, we are able to melt the 2 tables into 1.
For instruction on how to use 'reshape' : https://cran.r-project.org/web/packages/reshape/reshape.pdf
#useing reshape package
colmelt <- melt(color.df)
cirmelt <- melt(cir.df)
#check name and rename
names(cirmelt) <-c("gChrom_variable", "gChromVar")
colnames(colmelt) <- c("CellType" , "variables", "LDSC")
newdf <- cbind(colmelt, cirmelt)
ggplot() is used for plotting (in particular geom_point()).
colour= ifelse(x>1.645, "black", "white"), #codes for the outline of the circles. If gChromVar is larger then 1.645, then it will have a black outline
geom_point(data = newdf[newdf$LDSC >1.645, ],aes(x=CellType, y=gChrom_variable),shape = "*", size=8)+ # if LDSC is over 1.645 it will have a *
x <- as.numeric(newdf$gChromVar)
class(x)
saveplot <- ggplot(newdf, aes(CellType,gChrom_variable)) +
geom_point(aes(fill=LDSC, size= gChromVar),
colour= ifelse(x>1.645, "black", "white"),
shape=21) +
labs( y="",
x="",
title="")+
scale_size(range = c(0.8,13.8)) +
theme_bw()+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.x = element_text(angle = 60, hjust = 1,
size = 12),
axis.text.y = element_text(size=12))
Nplot <- saveplot +
geom_point(data = newdf[newdf$LDSC >1.645, ],aes(x=CellType, y=gChrom_variable),shape = "*", size=8)+
scale_fill_gradient2(low = "deepskyblue2", high = "red", mid="white")
ggsave(filename = "1CombinedPlot.png", Nplot, width = 12, height = 9.9, dpi = 300, units = "in")
CombinePlot_OG.R
#setup
rm(list=ls())
#install.packages('reshape')
library(ggplot2)
library(gplots)
library(reshape)
setwd("/Users/niniliu/Desktop/2020lab")
DDir='/Users/niniliu/Desktop/2020lab'
#scp data from
#"/space/chen-syn01/1/data/wed009/gwas_chromatin/pheno_celltype_heatmap/allregions_ldsc_celltypes_coefficientzscore.txt" (using colors)
#"/space/chen-syn01/1/data/wed009/gwas_chromatin/pheno_celltype_heatmap/gChromVar_MOp_organoid_heatmap_mat.txt" (using circle size)
circleDat <- read.delim("~/Desktop/2020lab/gChromVar_MOp_organoid_heatmap_mat.txt")
#View(gChromVar_MOp_organoid_heatmap_mat)
colorDat <- read.delim("~/Desktop/2020lab/allregions_ldsc_celltypes_coefficientzscore.txt")
#View(allregions_ldsc_celltypes_coefficientzscore)
#change order of colorDAt
##colorDat$CellTypes
orderlist <- c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT", "Ex_L5-6_NP", "Ex_L5_ET",
"Ex_L5_IT", "Ex_L6b", "Ex_L6_CT", "Ex_L6_IT_Car3", "Ex_L6_IT", "In_LAMP5", "In_PVALB",
"In_SNCG", "In_SST_CHODL", "In_SST", "In_VIP", "In_VLMC")
#colorDat$Names <- c("Astro", "Endo", "Ex_L2-3_IT", "Ex_L5-6_NP", "Ex_L5_ET", "Ex_L5_IT","Ex_L6b",
# "Ex_L6_CT", "Ex_L6_IT_Car3", "Ex_L6_IT","In_LAMP5", "In_PVALB","In_SNCG",
# "In_SST_CHODL", "In_SST", "In_VIP", "In_VLMC" , "Micro-PVM", "Oligo", "OPC")
match(colorDat$CellTypes, orderlist)
rownames(colorDat) <- match(colorDat$CellTypes, orderlist) #add a new column that specpfiy the desired order
colorDat$order <- match(colorDat$CellTypes, orderlist)
colorDat$CellTypes <- factor(colorDat$CellTypes,
levels = c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT", "Ex_L5-6_NP", "Ex_L5_ET",
"Ex_L5_IT", "Ex_L6b", "Ex_L6_CT", "Ex_L6_IT_Car3", "Ex_L6_IT", "In_LAMP5", "In_PVALB",
"In_SNCG", "In_SST_CHODL", "In_SST", "In_VIP", "In_VLMC"))
col.df <- colorDat[order(colorDat$order),] #the df will work with
#make dat match circle
circleDat[0,1:20] #cirle 1-20 collumn
circle.df <- as.data.frame(t(circleDat[1:24, 1:20])) #make short list and turn
#nammeslist <- c("Astro", "Endo", "Micro.PVM", "Oligo", "OPC", "Ex_L2.3.IT", "Ex_L5.ET",
# "EX_L5.IT", "Ex_L5.6.NP", "Ex_L6.CT", "Ex_L6.IT", "Ex_L6.IT.Car3","Ex_L6b",
# " In_LAMP5", "In_PVALB", "In_SNCG", "In_SST", "In_SST.CHODL", "In_VIP", "In_VLMC")
corr_nameslist <- c("Astro", "Endo", "Micro-PVM", "Oligo", "OPC", "Ex_L2-3_IT", "Ex_L5_ET",
"Ex_L5_IT", "Ex_L5-6_NP", "Ex_L6_CT", "Ex_L6_IT", "Ex_L6_IT_Car3","Ex_L6b",
"In_LAMP5", "In_PVALB", "In_SNCG", "In_SST", "In_SST_CHODL", "In_VIP", "In_VLMC")
circle.df$names <-corr_nameslist #this is the circlces name list, corrected with minor differenes with other name list
#random check
circle.df[20,]
#match and organize names
circle.df$order <- match(circle.df$names, orderlist)
cirorder <- circle.df[order(circle.df$order),] #reorder
cir.df <- cirorder[0:20,1:24] #clean out the order row
#get rid of order column in col.df
color.df <- col.df[,0:25]
#useing reshape package
colmelt <- melt(color.df)
cirmelt <- melt(cir.df)
#check name and rename
names(cirmelt) <-c("gChrom_variable", "gChromVar")
colnames(colmelt) <- c("CellType" , "variables", "LDSC")
newdf <- cbind(colmelt, cirmelt)
#######
x <- as.numeric(newdf$gChromVar)
class(x)
saveplot <- ggplot(newdf, aes(CellType,gChrom_variable)) +
geom_point(aes(fill=LDSC, size= gChromVar),
colour= ifelse(x>1.645, "black", "white"),
shape=21) +
labs( y="",
x="",
title="")+
scale_size(range = c(0.8,13.8)) +
theme_bw()+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.text.x = element_text(angle = 60, hjust = 1,
size = 12),
axis.text.y = element_text(size=12))
Nplot <- saveplot +
geom_point(data = newdf[newdf$LDSC >1.645, ],aes(x=CellType, y=gChrom_variable),shape = "*", size=8)+
scale_fill_gradient2(low = "deepskyblue2", high = "red", mid="white")
ggsave(filename = "1CombinedPlot.png", Nplot, width = 12, height = 9.9, dpi = 300, units = "in")