Base Graphics

Building Graphs

Incremental building of features into graphs.

Less is more

More visualization tips

References

  1. Graphical parameters
  2. Colors
  3. Named colors - type colors()
  4. Using color scales and plattes in R
  5. ?plot
  6. ?par
  7. Specific options e.g. ?pch

Basic plots

head(mtcars)
mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Datsun 71022.84108933.852.3218.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.4417.020032
Valiant18.162251052.763.4620.221031
with(mtcars, plot(wt, mpg))
_images/BaseGraphics_7_0.png
scatter.smooth(mtcars$wt, mtcars$mpg)
_images/BaseGraphics_8_0.png
df <- mtcars[order(mtcars$wt),]
with(df, plot(wt, mpg, type="b"))
_images/BaseGraphics_9_0.png
with(mtcars, hist(mpg, breaks=10))
_images/BaseGraphics_10_0.png
plot(density(mtcars$mpg), main="Density plot")
_images/BaseGraphics_11_0.png
attach(mtcars)
hist(mpg, breaks=10, probability = TRUE, main="")
rug(mpg)
x <- seq(min(mpg), max(mpg), length.out = 50)
lines(x, dnorm(x, mean=mean(x), sd=sd(x)), col="red", lwd=2)
detach(mtcars)
_images/BaseGraphics_12_0.png
with(mtcars, pie(table(carb)))
_images/BaseGraphics_13_0.png
with(mtcars, barplot(table(carb)))
_images/BaseGraphics_14_0.png
with(mtcars, barplot(table(carb), horiz=TRUE))
_images/BaseGraphics_15_0.png
attach(mtcars)
(tbl <- table(carb, am))
barplot(tbl, beside=TRUE, legend=rownames(tbl), col=heat.colors(carb))
detach(mtcars)
BaseGraphics_files/BaseGraphics_16_0.png
    am
carb 0 1
   1 3 4
   2 6 4
   3 3 0
   4 7 3
   6 0 1
   8 0 1
boxplot(log1p(mtcars))
_images/BaseGraphics_17_0.png
df <- mtcars[order(-mtcars$mpg),]
dotchart(df$mpg, labels=row.names(df))
_images/BaseGraphics_18_0.png
dotchart(df$mpg, labels=row.names(df), groups=df$cyl, color=df$cyl, pch=19)
_images/BaseGraphics_19_0.png
pairs(~mpg + drat + wt, data=mtcars)
_images/BaseGraphics_20_0.png

Building plots

  • Local customization of plots
  • Glaobl customization of plots
  • Adding more stuff to a plot
    • points
    • lines
    • text
    • mathematical expressions
  • Combining plots
  • Saving plots

Scatterplot

df <- mtcars[order(mtcars$wt),]
attach(df)
plot(wt, mpg)
detach(df)
_images/BaseGraphics_23_0.png

Combining plots

par(mfrow=c(2,2))
plot(rnorm(10))
plot(rnorm(10))
plot(rnorm(10))
plot(rnorm(10))
_images/BaseGraphics_25_0.png
(m <- matrix(c(1,1,2,3), 2, 2, byrow=TRUE))
11
23
layout(m)
plot(rnorm(10))
plot(rnorm(10))
plot(rnorm(10))
_images/BaseGraphics_27_0.png
layout(m, widths=c(1,2), heights=c(2,1))
plot(rnorm(10))
plot(rnorm(10))
plot(rnorm(10))
_images/BaseGraphics_28_0.png
orig <- par(no.readonly=TRUE)
x <- rnorm(10)
y <- rnorm(10)
par(fig = c(0, 0.8, 0, 0.8))
plot(x, y)
par(fig = c(0, 0.8, 0.6, 1), new=TRUE)
plot(density(x), axes=FALSE, main="", xlab="")
par(fig = c(0.6, 1, 0, 0.8), new=TRUE)
boxplot(y, axes=FALSE)
par(orig)
_images/BaseGraphics_29_0.png

Plotting Graphcs and Heatmpas

Graphs

It may be necessary to install first

install.packages("igraphdata", repos = "http://cran.r-project.org")
library(igraph)
library(igraphdata)
data(macaque)
plot(macaque, layout=layout.auto, vertex.shape="circle",
     vertex.size=8, edge.arrow.size=0.5, vertex.label=NA)
_images/MiscGraphics_4_0.png

Heatmap

heatmap(as.matrix(mtcars), Rowv = NA, Colv = NA, scale = "column")
_images/MiscGraphics_6_0.png

Heatmap with dendrogram

heatmap(as.matrix(mtcars), scale = "column")
_images/MiscGraphics_8_0.png

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.

library(pheatmap)
pheatmap(as.matrix(mtcars), scale = "column")
_images/MiscGraphics_11_0.png
library(gplots)
heatmap.2(as.matrix(mtcars), scale = "column", col=redgreen)
_images/MiscGraphics_13_0.png

Work!

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

suppressMessages(library(genefilter))

Perform noise discovery

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)