These exercises cover the sections on Plotting in R PlottingInR.
Please load the dateset SubsetRlog_7D.csv from "data/SubsetRlog_7D.csv"
read.csv()
function to load SubsetRlog_7D.csv. If you failed to import SubsetRlog_7D.csv to R, please check whether you set up your work directory properly by using getwd()
and setwd()
# This is the work directory I am using and yours will be different
# setwd("/Volumes/bioinfomatics$/yfwang/BioinformaticsCore/GitHub/LMS_PlottingInR/course/")
exp_Data <- read.csv("data/SubsetRlog_7D.csv",row.names = 1)
head(exp_Data)
## DOX24H_1 DOX24H_2 DOX24H_3 DOX7D_1 DOX7D_2 DOX7D_3
## ENSMUSG00000050711 12.109594 12.155740 12.089761 13.891323 13.856755 14.095029
## ENSMUSG00000026463 12.288243 12.069297 11.934777 11.971843 11.908514 11.748345
## ENSMUSG00000051079 7.177476 7.282678 7.415341 8.097972 8.558204 8.653715
## ENSMUSG00000037995 9.178713 9.370469 9.047744 7.834046 7.667787 7.540055
## ENSMUSG00000039910 9.024315 8.790573 8.878026 9.497840 9.751352 9.731984
## ENSMUSG00000019880 8.308681 8.701553 8.403844 9.026944 9.351475 9.373127
## VEH24H_1 VEH24H_2 VEH24H_3 VEH7D_1 VEH7D_2 VEH7D_3
## ENSMUSG00000050711 12.361107 12.560950 12.404746 13.640879 13.552177 13.578203
## ENSMUSG00000026463 12.543844 12.334998 12.195093 12.357483 12.181816 12.315383
## ENSMUSG00000051079 6.761927 6.861818 6.899967 7.445987 7.440482 7.938914
## ENSMUSG00000037995 9.222387 9.152175 9.299462 8.064490 7.994608 8.025586
## ENSMUSG00000039910 9.257527 9.246675 9.442477 9.346837 9.238912 9.323775
## ENSMUSG00000019880 8.305506 8.296453 8.429132 8.762056 8.791535 8.877247
class(exp_Data)
## [1] "data.frame"
scale()
function to perform the z-score transform on each genes?scale
to see how to use this function properly and you might need t()
to perform this analysis properly?scale
?t
scaled_max <- t(scale(t(exp_Data)))
Heatmap()
function from R bioconductor packages ComplexHeatmap and RColorBrewer to create the heatmap below:note: TimePoint colour codes are from brewer.pal(10, "Paired")[c(9:10)]
and Treatment colour codes are from brewer.pal(6, "Dark2")[c(1,2)]
library("ComplexHeatmap")
library("RColorBrewer")
# check the order of the columns and show Veh.24hr, Dox.24hr, Veh.7D and Dox.7D samples
head(scaled_max)
## DOX24H_1 DOX24H_2 DOX24H_3 DOX7D_1 DOX7D_2
## ENSMUSG00000050711 -1.1422782 -1.08467574 -1.1670354 1.0817870 1.038637
## ENSMUSG00000026463 0.5810754 -0.36760259 -0.9504706 -0.7898684 -1.064266
## ENSMUSG00000051079 -0.5768369 -0.41151302 -0.2030349 0.8697113 1.592960
## ENSMUSG00000037995 0.8875988 1.15123888 0.7075324 -0.9611463 -1.189731
## ENSMUSG00000039910 -0.9069069 -1.69238691 -1.3985060 0.6843528 1.536270
## ENSMUSG00000019880 -1.0572857 -0.04486377 -0.8120520 0.7936580 1.629967
## DOX7D_3 VEH24H_1 VEH24H_2 VEH24H_3 VEH7D_1
## ENSMUSG00000050711 1.336066 -0.8283242 -0.5788677 -0.7738506 0.7691668
## ENSMUSG00000026463 -1.758269 1.6885784 0.7836612 0.1774642 0.8810887
## ENSMUSG00000051079 1.743055 -1.2298673 -1.0728892 -1.0129387 -0.1548750
## ENSMUSG00000037995 -1.365347 0.9476440 0.8511121 1.0536132 -0.6443163
## ENSMUSG00000039910 1.471184 -0.1232086 -0.1596752 0.4983090 0.1769150
## ENSMUSG00000019880 1.685764 -1.0654662 -1.0887967 -0.7468858 0.1110505
## VEH7D_2 VEH7D_3
## ENSMUSG00000050711 0.6584434 0.69093158
## ENSMUSG00000026463 0.1199358 0.69867306
## ENSMUSG00000051079 -0.1635261 0.61975449
## ENSMUSG00000037995 -0.7403945 -0.69780421
## ENSMUSG00000039910 -0.1857634 0.09941564
## ENSMUSG00000019880 0.1870174 0.40789275
Sam_Veh24H <- grep("VEH24H",colnames(scaled_max))
Sam_Veh24H
## [1] 7 8 9
Sam_Dox24H <- grep("DOX24H",colnames(scaled_max))
Sam_Dox24H
## [1] 1 2 3
Sam_Veh7D <- grep("VEH7D",colnames(scaled_max))
Sam_Veh7D
## [1] 10 11 12
Sam_Dox7D <- grep("DOX7D",colnames(scaled_max))
Sam_Dox7D
## [1] 4 5 6
scaled_max_ok <- scaled_max[,c(Sam_Veh24H,Sam_Dox24H,
Sam_Veh7D,Sam_Dox7D)]
head(scaled_max_ok)
## VEH24H_1 VEH24H_2 VEH24H_3 DOX24H_1 DOX24H_2
## ENSMUSG00000050711 -0.8283242 -0.5788677 -0.7738506 -1.1422782 -1.08467574
## ENSMUSG00000026463 1.6885784 0.7836612 0.1774642 0.5810754 -0.36760259
## ENSMUSG00000051079 -1.2298673 -1.0728892 -1.0129387 -0.5768369 -0.41151302
## ENSMUSG00000037995 0.9476440 0.8511121 1.0536132 0.8875988 1.15123888
## ENSMUSG00000039910 -0.1232086 -0.1596752 0.4983090 -0.9069069 -1.69238691
## ENSMUSG00000019880 -1.0654662 -1.0887967 -0.7468858 -1.0572857 -0.04486377
## DOX24H_3 VEH7D_1 VEH7D_2 VEH7D_3 DOX7D_1
## ENSMUSG00000050711 -1.1670354 0.7691668 0.6584434 0.69093158 1.0817870
## ENSMUSG00000026463 -0.9504706 0.8810887 0.1199358 0.69867306 -0.7898684
## ENSMUSG00000051079 -0.2030349 -0.1548750 -0.1635261 0.61975449 0.8697113
## ENSMUSG00000037995 0.7075324 -0.6443163 -0.7403945 -0.69780421 -0.9611463
## ENSMUSG00000039910 -1.3985060 0.1769150 -0.1857634 0.09941564 0.6843528
## ENSMUSG00000019880 -0.8120520 0.1110505 0.1870174 0.40789275 0.7936580
## DOX7D_2 DOX7D_3
## ENSMUSG00000050711 1.038637 1.336066
## ENSMUSG00000026463 -1.064266 -1.758269
## ENSMUSG00000051079 1.592960 1.743055
## ENSMUSG00000037995 -1.189731 -1.365347
## ENSMUSG00000039910 1.536270 1.471184
## ENSMUSG00000019880 1.629967 1.685764
ht_tp <- Heatmap(scaled_max_ok,
cluster_columns = F,show_row_names = F,
col = rev(colorRampPalette(brewer.pal(8, "RdBu"))(25)))
draw(ht_tp)
# add top annotation
# create top annotation
anno_alltp <- data.frame(SampleNames=colnames(scaled_max_ok))
anno_alltp$TimePoint <- sub("(.+)([2|7].+)(_)(.+)","\\2",anno_alltp$SampleNames)
anno_alltp$TimePoint <- as.factor(anno_alltp$TimePoint)
anno_alltp$Treatment <- sub("(.+)([2|7].+)(_)(.+)","\\1",anno_alltp$SampleNames)
anno_alltp$Treatment <- as.factor(anno_alltp$Treatment)
alltime_col <- brewer.pal(10, "Paired")[c(9:10)]
names(alltime_col) <- levels(anno_alltp$TimePoint)
trt_col <- brewer.pal(6, "Dark2")[c(1,2)]
names(trt_col) <- levels(anno_alltp$Treatment)
allcol_ha = HeatmapAnnotation(TimePoint = anno_alltp$TimePoint,
Treatment = anno_alltp$Treatment,
col = list(TimePoint = alltime_col,
Treatment = trt_col))
ht_tp2 <- Heatmap(scaled_max_ok,
cluster_columns = F,show_row_names = F,
show_column_names = F,
col = rev(colorRampPalette(brewer.pal(8, "RdBu"))(25)),
top_annotation = allcol_ha)
draw(ht_tp2)
pdf_name <- "heatmap_exercise.pdf"
png_name <- "heatmap_exercise.png"
pdf(pdf_name, width=4.5, height=5)
draw(ht_tp2)
dev.off()
## quartz_off_screen
## 2
png(png_name)
draw(ht_tp2)
dev.off()
## quartz_off_screen
## 2