# Install packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
# Load packages
library(ggplot2)
Custom Heatmap
Custom Heatmap, directly plot a heatmap based on the given data.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
ggplot2
Data Preparation
The case data is a grayscale image of the official logo of hiplot.org.
# Load data
<- read.delim("files/Hiplot/034-custom-heat-map-data.txt", header = T)
data
# convert data structure
<- as.matrix(data[, 2:ncol(data)])
draw_data <- nrow(draw_data)
row_num <- ncol(draw_data)
col_num <- colnames(data)
col_labels <- col_labels[2:ncol(data)]
col_labels <- data$name
row_labels rm(data)
<- expand.grid(row = 1:row_num, col = 1:col_num)
df $value <- c(draw_data)
df
# View data
head(df)
row col value
1 1 1 236
2 2 1 236
3 3 1 236
4 4 1 236
5 5 1 236
6 6 1 236
Visualization
# Custom Heatmap
<- ggplot(df, aes(x = col, y = row, fill = value)) +
p geom_point(shape = 21, size = 8, aes(fill = value), color = "white") +
scale_fill_gradient(low = "#DDDDDD", high = "#0000F5") +
guides(fill = guide_colorbar(title = "Value")) +
theme(
panel.background = element_rect(fill = "white"),
panel.grid = element_blank(),
axis.text = element_text(size = 10),
axis.ticks = element_blank(),
axis.title = element_blank()
+
) scale_x_continuous(breaks = 1:col_num, labels = col_labels, position = "top") +
scale_y_reverse(breaks = 1:row_num, labels = row_labels, position = "left")
p
