Plotting Graphcs and Heatmpas

Graphs

It may be necessary to install first

install.packages("igraphdata", repos = "http://cran.r-project.org")
In [1]:
library(igraph)
library(igraphdata)
Error in library(igraph): there is no package called ‘igraph’

In [2]:
data(macaque)
In [3]:
plot(macaque, layout=layout.auto, vertex.shape="circle",
     vertex.size=8, edge.arrow.size=0.5, vertex.label=NA)
Error in xy.coords(x, y, xlabel, ylabel, log): 'x' is a list, but does not have components 'x' and 'y'

Heatmap

In [4]:
heatmap(as.matrix(mtcars), Rowv = NA, Colv = NA, scale = "column")

Heatmap with dendrogram

In [5]:
heatmap(as.matrix(mtcars), scale = "column")

Fancy heatmaps

Also see d3heatmaps if you want an interactive heatmap. This does not seem to work within the notebook but will work in RStudio or an R script.

In [6]:
library(pheatmap)
In [7]:
pheatmap(as.matrix(mtcars), scale = "column")
In [8]:
library(gplots)

Attaching package: ‘gplots’

The following object is masked from ‘package:stats’:

    lowess

In [9]:
heatmap.2(as.matrix(mtcars), scale = "column", col=redgreen)

Work!

Here we will try to replicate the noise discovery heatmap shown in the statistics class.

In [10]:
suppressMessages(library(genefilter))

Perform noise discovery

In [11]:
set.seed(123)

n <- 20 # number of subjects
m <- 20000 # number of genes
alpha <- 0.005 # significance level

# create a matrix of gene expression values with m rows and 2*n columns
M <- matrix(rnorm(2*n*m), m, 2*n)

# give row and column names
rownames(M) <- paste("G", 1:m, sep="")
colnames(M) <- paste("id", 1:(2*n), sep="")

# assign subjects inot equal sized groups
grp <- factor(rep(0:1, c(n, n)))

# calculate p-value using t-test for mean experession value of each gene
pvals <- rowttests(M, grp)$p.value

# extract the genes which meet the specified significance level
hits <- M[pvals < alpha,]
  • Use pheatmap to plot a heatmap
  • Remove the row names (Google or use R’s built-in help to figure out to do this)
  • Use this color palette to map expression values to a red-blakc-green scale

colorRampPalette(c("red3", "black", "green3"))(50)

In [ ]: