首页 > 解决方案 > R 带圆圈的热图

问题描述

我想在 R 中使用圆圈生成矩阵的热图可视化,以便让圆圈的颜色和直径都提供信息。看起来像这样的东西: 带有圆圈的示例 R 热图

这种绘图在某些计算生物学实验室中被称为“泡泡图”,但我找不到 R 函数/包来做它。

有任何想法吗?谢谢!

标签: rheatmap

解决方案


不确定是否有提供开箱即用的软件包,但仅使用ggplot2它可以像这样实现:

library(ggplot2)

set.seed(42)

d <- data.frame(
  x = rep(paste("Team", LETTERS[1:8]), 4),
  y = rep(paste("Task", 1:4), each = 8),
  value = runif(32)
)

ggplot(d, aes(x, forcats::fct_rev(y), fill = value, size = value)) +
  geom_point(shape = 21, stroke = 0) +
  geom_hline(yintercept = seq(.5, 4.5, 1), size = .2) +
  scale_x_discrete(position = "top") +
  scale_radius(range = c(1, 15)) +
  scale_fill_gradient(low = "orange", high = "blue", breaks = c(0, .5, 1), labels = c("Great", "OK", "Bad"), limits = c(0, 1)) +
  theme_minimal() +
  theme(legend.position = "bottom", 
        panel.grid.major = element_blank(),
        legend.text = element_text(size = 8),
        legend.title = element_text(size = 8)) +
  guides(size = guide_legend(override.aes = list(fill = NA, color = "black", stroke = .25), 
                             label.position = "bottom",
                             title.position = "right", 
                             order = 1),
         fill = guide_colorbar(ticks.colour = NA, title.position = "top", order = 2)) +
  labs(size = "Area = Time Spent", fill = "Score:", x = NULL, y = NULL)


推荐阅读