您可以将ggplot2 中的geom_tile() 几何与scale_fill_fermenter() 一起使用。您可以调整标尺的 breaks 参数,使其具有您想要的不同断点的任何值(添加更大/更小的限制可防止标尺末端的超长类别)。
graphmat <- function(MAT) {
mat <- as_tibble(MAT, .name_repair = ~paste0("V", 1:ncol(MAT))) %>%
mutate(row = 1:nrow(.)) %>%
pivot_longer(cols = starts_with("V"),
names_pattern = "V(.+)", names_to = "col") %>%
mutate(col = as.numeric(col))
ggplot(mat, aes(x = row, y = col, fill = value)) +
geom_tile() +
scale_fill_fermenter(
breaks = c(-50, -5, 0, 5, 99),
limits = c(-50.001, 99.001),
palette = "RdBu"
) +
scale_y_reverse() +
theme_minimal() +
theme(
legend.position = 'top',
legend.key.width = unit(70, "pt"),
legend.key.height = unit(10, "pt"),
)
}
MAT <- matrix(c(100, 7, 0, 0, 49, 0, 0, 0, -49), nrow = 3, ncol = 3)
graphmat(MAT)
BIGMAT <- matrix(round(runif(500^2)*100, 2), nrow = 500, ncol = 500)
graphmat(BIGMAT)