# Install packages
if (!requireNamespace("VennDiagram", quietly = TRUE)) {
install.packages("VennDiagram")
}if (!requireNamespace("ComplexHeatmap", quietly = TRUE)) {
install_github("jokergoo/ComplexHeatmap")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
# Load packages
library(VennDiagram)
library(ComplexHeatmap)
library(ggplotify)
library(ggplot2)
Upset Plot
Hiplot website
This page is the tutorial for source code version of the Hiplot Upset Plot
plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
Upset can be used to show the interactive relationship between collections.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
VennDiagram
;ComplexHeatmap
;ggplotify
;ggplot2
Data Preparation
There are two types of data tables: list and binary. The list format is that each column is a set and contains all the elements corresponding to the set. In the binary format, the first column is all the elements of all sets, and the subsequent columns are a numeric matrix composed of 0 and 1. 1 indicates that the corresponding row element exists in a certain set, and 0 indicates that it does not exist.
# Load data
<- read.delim("files/Hiplot/177-upset-plot-data.txt", header = T)
data
# convert data structure
for (i in seq_len(ncol(data))) {
is.na(data[, i]), i] <- ""
data[
}<- as.list(data)
data2 <- lapply(data2, function(x) {x[x != ""]})
data2 <- list_to_matrix(data2)
data2 = make_comb_mat(data2, mode = "distinct")
m = set_size(m)
ss = comb_size(m)
cs <- order(ss)
set_order <- order(comb_degree(m), -cs)
comb_order
# View data
head(data)
Set1 Set2 Set3 Set4 Set5 Set6 Set7 Set8
1 ISG15 HES5 DVL1 MATP6P1 FAM132A FAM132A FAM132A TNFRSF4
2 TTLL10 AURKAIP1 ARHGEF16 MIR551A AGRN MIR551A WBP1LP6 WASH7P
3 HES4 LINC00982 OR4F16 C1orf222 WBP1LP6 MIR200B PANK4 TMEM52
4 OR4G4P FAM87B SKI MIR200B KLHL17 ATAD3C OR4G4P MMP23B
5 MND2P28 SKI WASH7P LINC00115 FAM41C ANKRD65 SSU72 CDK11B
6 FAM87B GABRD MEGF6 ATAD3B PANK4 LINC01128 MND2P28 C1orf170
Visualization
# Upset Plot
<- as.ggplot(function(){
p <- HeatmapAnnotation(
top_annotation Intersections = anno_barplot(
ylim = c(0, max(cs)*1.1),
cs, border = FALSE,
gp = gpar(fill = "#000000", fontsize = 10),
height = unit(5, "cm")
), annotation_name_side = "left",
annotation_name_rot = 90
)
<- rowAnnotation(
left_annotation Numbers = anno_barplot(-ss, axis_param = list(
at = seq(-max(ss), 0, round(max(ss)/5)),
labels = rev(seq(0, max(ss), round(max(ss)/5))),
labels_rot = 0),
baseline = 0,
border = FALSE,
gp = gpar(fill = "#000000", fontsize = 10),
width = unit(4, "cm")
),set_name = anno_text(set_name(m), location = 0.5, just = "center",
width = max_text_width(set_name(m)) + unit(5, "mm"))
)
= UpSet(m, comb_col = "#000000", bg_col = "#F0F0F0", bg_pt_col = "#CCCCCC",
ht pt_size = unit(3, "mm"), lwd = 2, set_order = set_order,
comb_order = comb_order, top_annotation = top_annotation,
left_annotation = left_annotation, right_annotation = NULL,
show_row_names = FALSE)
= draw(ht)
ht = column_order(ht)
od decorate_annotation("Intersections", {
grid.text(cs[od], x = seq_along(cs), y = unit(cs[od], "native") + unit(2, "pt"),
default.units = "native", just = c("left", "bottom"),
gp = gpar(fontsize = 10, col = "#000000",
fontfamily = "Arial"), hjust = 0.5)
})
})<- p + ggtitle("Upset Plot") +
p theme(plot.title = element_text(hjust = 0.6))
p
