set.seed(1234)
data(iris)
x <- as.matrix(iris[sample(nrow(iris), 20) , -5])
pimage(x)
# Show all labels and flip axes, reverse columns, or change colors
pimage(x, prop = TRUE)
pimage(x, flip_axes = TRUE)
pimage(x, reverse_columns = TRUE)
pimage(x, col = grays(100))
# A matrix with positive and negative values
x_scaled <- scale(x)
pimage(x_scaled)
# Use reordering
pimage(x_scaled, order = TRUE)
pimage(x_scaled, order = "Heatmap")
## Example: Distance Matrix
# Show a reordered distance matrix (distances between rows).
# Dark means low distance. The aspect ratio is automatically fixed to 1:1
# using prop = TRUE
d <- dist(x)
pimage(d)
pimage(d, order = TRUE)
# Supress the upper triangle and diagonal
pimage(d, order = TRUE, upper = FALSE, diag = FALSE)
# Show only distances that are smaller than 2 using limits on z.
pimage(d, order = TRUE, zlim = c(0, 3))
## Example: Correlation Matrix
# we calculate correlation between rows and seriate the matrix
# and seriate by converting the correlations into distances.
# pimage reorders then rows and columns with c(o, o).
r <- cor(t(x))
o <- seriate(as.dist(sqrt(1 - r)))
pimage(r, order = c(o, o),
upper = FALSE, diag = FALSE,
zlim = c(-1, 1),
reverse_columns = TRUE,
main = "Correlation matrix")
# Add to the plot using functions in package grid
# Note: pop = FALSE allows us to manipulate viewports
library("grid")
pimage(x, order = TRUE, pop = FALSE)
# available viewports are: "main", "colorkey", "plot", "image"
current.vpTree()
# Highlight cell 2/2 with a red arrow
# Note: columns are x and rows are y.
downViewport(name = "image")
grid.lines(x = c(1, 2), y = c(-1, 2), arrow = arrow(),
default.units = "native", gp = gpar(col = "red", lwd = 3))
# add a red box around the first 4 rows of the 2nd column
grid.rect(x = 1 + .5 , y = 4 + .5, width = 1, height = 4,
hjust = 0, vjust = 1,
default.units = "native", gp = gpar(col = "red", lwd = 3, fill = NA))
## remove the viewports
popViewport(0)
## put several pimages on a page (use grid viewports and newpage = FALSE)
# set up grid layout
library(grid)
grid.newpage()
top_vp <- viewport(layout = grid.layout(nrow = 1, ncol = 2,
widths = unit(c(.4, .6), unit = "npc")))
col1_vp <- viewport(layout.pos.row = 1, layout.pos.col = 1, name = "col1_vp")
col2_vp <- viewport(layout.pos.row = 1, layout.pos.col = 2, name = "col2_vp")
splot <- vpTree(top_vp, vpList(col1_vp, col2_vp))
pushViewport(splot)
seekViewport("col1_vp")
o <- seriate(d)
pimage(x, c(o, NA), col_labels = FALSE, main = "Data",
newpage = FALSE)
seekViewport("col2_vp")
## add the reordered dissimilarity matrix for rows
pimage(d, o, main = "Distances",
newpage = FALSE)
popViewport(0)
##-------------------------------------------------------------
## ggplot2 Examples
if (require("ggplot2")) {
library("ggplot2")
set.seed(1234)
data(iris)
x <- as.matrix(iris[sample(nrow(iris), 20) , -5])
ggpimage(x)
# Show all labels and flip axes, reverse columns
ggpimage(x, prop = TRUE)
ggpimage(x, flip_axes = TRUE)
ggpimage(x, reverse_columns = TRUE)
# A matrix with positive and negative values
x_scaled <- scale(x)
ggpimage(x_scaled)
# Use reordering
ggpimage(x_scaled, order = TRUE)
ggpimage(x_scaled, order = "Heatmap")
## Example: Distance Matrix
# Show a reordered distance matrix (distances between rows).
# Dark means low distance. The aspect ratio is automatically fixed to 1:1
# using prop = TRUE
d <- dist(x)
ggpimage(d)
ggpimage(d, order = TRUE)
# Supress the upper triangle and diagonal
ggpimage(d, order = TRUE, upper = FALSE, diag = FALSE)
# Show only distances that are smaller than 2 using limits on z.
ggpimage(d, order = TRUE, zlim = c(0, 2))
## Example: Correlation Matrix
# we calculate correlation between rows and seriate the matrix
r <- cor(t(x))
o <- seriate(as.dist(sqrt(1 - r)))
ggpimage(r, order = c(o, o),
upper = FALSE, diag = FALSE,
zlim = c(-1, 1),
reverse_columns = TRUE) + labs(title = "Correlation matrix")
## Example: Custom themes and colors
# Reorder matrix, use custom colors, add a title,
# and hide colorkey.
ggpimage(x) +
theme(legend.position = "none") +
labs(title = "Random Data") + xlab("Variables")
# Add lines
ggpimage(x) +
geom_hline(yintercept = seq(0, nrow(x)) + .5) +
geom_vline(xintercept = seq(0, ncol(x)) + .5)
# Use ggplot2 themes with theme_set
old_theme <- theme_set(theme_linedraw())
ggpimage(d)
theme_set(old_theme)
# Use custom color palettes: Gray scale, Colorbrewer (provided in ggplot2) and colorspace
ggpimage(d, order = seriate(d), upper_tri = FALSE) +
scale_fill_gradient(low = "black", high = "white", na.value = "white")
ggpimage(d, order = seriate(d), upper_tri = FALSE) +
scale_fill_distiller(palette = "Spectral", direction = +1, na.value = "white")
ggpimage(d, order = seriate(d), upper_tri = FALSE) +
colorspace::scale_fill_continuous_sequential("Reds", rev = FALSE, na.value = "white")
}
Run the code above in your browser using DataLab