library(pheatmap)
library(viridis)
library(ggplot2)
library(viridis)
library(reshape2)
library(grid)
data <- read.table("ANIm_percentage_identity.tab", sep = "\t", header = T, row.names = 1)
# Convert the data to a matrix
data_matrix <- as.matrix(data)
# Multiply all values by 100 for percentage
data_matrix <- data_matrix * 100
# Melt the data to long format
data_long <- melt(data_matrix)
# Custom formatting to remove decimal places for whole numbers
formatted_numbers <- apply(data_matrix, c(1, 2), function(x) {
# Format numbers without decimal points
if (x %% 1 == 0) {
return(as.character(as.integer(x))) # Convert to integer and then to character
} else {
return(as.character(round(x, 1))) # Round to one decimal place for non-whole numbers
}
})
# Create the heatmap
heatmap_plot <- pheatmap(
data_matrix,
cluster_rows = TRUE, # Cluster the rows (to maintain order)
cluster_cols = TRUE, # Cluster the columns
clustering_method = "complete", # Complete linkage clustering
clustering_distance_rows = "euclidean", # Euclidean distance for rows
clustering_distance_cols = "euclidean", # Euclidean distance for columns
color = colorRampPalette(c("white", "orange", "red"))(100), # Gradient from white to orange to red
display_numbers = formatted_numbers, # Use custom formatted numbers
fontsize_number = 9, # Increase font size of values to 8
cellwidth = 20, # Increase cell width for better spacing
cellheight = 20, # Increase cell height for better spacing
scale = "none", # No scaling of ANI values
border_color = NA, # Remove borders around cells
angle_col = 90, # Rotate x-axis labels 90 degrees
legend = TRUE, # Title for the legend
treeheight_row = 200, # Increase row dendrogram height for larger branches
treeheight_col = 200, # Increase column dendrogram height for larger branches
show_colnames = TRUE, # Show x-axis labels
show_rownames = TRUE, # Show y-axis labels
margin = c(10, 10, 10, 10), # Increase margins (bottom, left, top, right)
number_color = "black"
)
# Save the heatmap to a PDF with appropriate dimensions
pdf("heatmap.pdf", width = 18, height = 18) # Adjust size as needed
# Print the heatmap to the PDF device
print(heatmap_plot)
# Close the PDF device
dev.off()